[JavaScript]跟随鼠标移动的DIV
关键事件
[body|img|...].onmousemove
关键代码
<div style="position:absolute;"></div>
obj.style.visibility = "visible";
div.style.left = event.x +getScrollL() + 10;//getScrollL()获取滚动条的Left值
div.style.top = event.y + getScrollT() + 10;//getScrollT()获取滚动条的Top值
div.style.left = event.x +getScrollL() + 10;//getScrollL()获取滚动条的Left值
div.style.top = event.y + getScrollT() + 10;//getScrollT()获取滚动条的Top值
附:获取滚动条参数的函数
/// <summary>
/// 获取滚动条的Left值
/// </summary>
/// <returns>scrollLeft值</returns>
function getScrollL()
{
var l
if (document.documentElement && document.documentElement.scrollTop)
{
l = document.documentElement.scrollLeft;
}
else if (document.body)
{
l = document.body.scrollLeft;
}
return l;
}
/// <summary>
/// 获取滚动条的Height值
/// </summary>
/// <returns>scrollHeight值</returns>
function getScrollH()
{
var h;
if (document.documentElement && document.documentElement.scrollTop)
{
h = document.documentElement.scrollHeight;
}
else if (document.body)
{
h = document.body.scrollHeight;
}
return h;
}
/// <summary>
/// 获取滚动条的Width值
/// </summary>
/// <returns>scrollWidth值</returns>
function getScrollW() {
var w;
if (document.documentElement && document.documentElement.scrollTop)
{
w = document.documentElement.scrollWidth;
}
else if (document.body)
{
w = document.body.scrollWidth;
}
return w;
}
/// <summary>
/// 获取滚动条的Top值
/// </summary>
/// <returns>scrollTop值</returns>
function getScrollT() {
var t;
if (document.documentElement && document.documentElement.scrollTop)
{
t = document.documentElement.scrollTop;
}
else if (document.body)
{
t = document.body.scrollTop;
}
return t;
}
/// 获取滚动条的Left值
/// </summary>
/// <returns>scrollLeft值</returns>
function getScrollL()
{
var l
if (document.documentElement && document.documentElement.scrollTop)
{
l = document.documentElement.scrollLeft;
}
else if (document.body)
{
l = document.body.scrollLeft;
}
return l;
}
/// <summary>
/// 获取滚动条的Height值
/// </summary>
/// <returns>scrollHeight值</returns>
function getScrollH()
{
var h;
if (document.documentElement && document.documentElement.scrollTop)
{
h = document.documentElement.scrollHeight;
}
else if (document.body)
{
h = document.body.scrollHeight;
}
return h;
}
/// <summary>
/// 获取滚动条的Width值
/// </summary>
/// <returns>scrollWidth值</returns>
function getScrollW() {
var w;
if (document.documentElement && document.documentElement.scrollTop)
{
w = document.documentElement.scrollWidth;
}
else if (document.body)
{
w = document.body.scrollWidth;
}
return w;
}
/// <summary>
/// 获取滚动条的Top值
/// </summary>
/// <returns>scrollTop值</returns>
function getScrollT() {
var t;
if (document.documentElement && document.documentElement.scrollTop)
{
t = document.documentElement.scrollTop;
}
else if (document.body)
{
t = document.body.scrollTop;
}
return t;
}