多兼容的JS获取鼠标坐标

//第一、相对于屏幕
function getMousePos(event) {
    var e = event || window.event;
    return {'x':e.screenX,'y':screenY}
}    
 
//第二、相对于浏览器
function getMousePos(event) {
    var e = event || window.event;
    return {'x':e.clientX,'y':clientY}
}
 
//第三、相对于文档
function getMousePos(event) {
    var e = event || window.event;
    var scrollX = document.documentElement.scrollLeft || document.body.scrollLeft;
    var scrollY = document.documentElement.scrollTop || document.body.scrollTop;
    var x = e.pageX || e.clientX + scrollX;
    var y = e.pageY || e.clientY + scrollY;
    //alert('x: ' + x + '\ny: ' + y);
    return { 'x': x, 'y': y };
}

 

posted @ 2016-11-25 13:36  Horan  阅读(253)  评论(0编辑  收藏  举报