獲得鼠標在網頁上的位置(x,y軸座標)
function mouseCoords(ev)
{
if(ev.pageX || ev.pageY)
{
return {x:ev.pageX, y:ev.pageY};
}
return {
x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
y:ev.clientY + document.body.scrollTop - document.body.clientTop
};
}
獲得網頁的有效頁面長度和寬度
function GetPageSize()
{
var re = {};
if (document.documentElement && document.documentElement.clientHeight)
{
var doc = document.documentElement;
re.width = (doc.clientWidth>doc.scrollWidth)?doc.clientWidth-1:doc.scrollWidth;
re.height = (doc.clientHeight>doc.scrollHeight)?doc.clientHeight:doc.scrollHeight;
}
else
{
var doc = document.body;
re.width = (window.innerWidth>doc.scrollWidth)?window.innerWidth:doc.scrollWidth;
re.height = (window.innerHeight>doc.scrollHeight)?window.innerHeight:doc.scrollHeight;
}
return re;
}
具體的應用:
function mouseMove(ev)
{
ev = ev || window.event;
var mousePos = mouseCoords(ev);
Divs.style.left = mousePos.x;
Divs.style.top = mousePos.y;
}