获得鼠标在网页上的位置(x,y轴座标)

 获得鼠标在网页上的位置(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;
}

发布了81 篇原创文章 · 获赞 5 · 访问量 9万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章