代码改变世界

jquery获取鼠标位置

2012-05-28 13:29  @影子@  阅读(942)  评论(0编辑  收藏  举报

 function getScrollLeft() {
    var d = document;
    return window.pageXOffset || d.documentElement.scrollLeft || d.body.scrollLeft;
};
function getScrollTop() {
    var d = document;
    return window.pageYOffset || d.documentElement.scrollTop || d.body.scrollTop;
};

var xy = {x:0, y:0};
// 监听当前网页的 mousemove 事件以获得鼠标的实时坐标
$(document).mousemove(function(e){
    e = window.event || e;
    xy.x = e.clientX;
    xy.y = e.clientY;
});
function getMousePosition(){
 return {
    x : getScrollLeft() + xy.x,
    y : getScrollTop() + xy.y
   };
};
$(document).mousedown(function (){
 var pos = getMousePosition();
 alert("x:"+pos.x+", y:"+pos.y);
});