如何在jquery / javascript中一起获取鼠标滚轮信息和鼠标位置

时间:2012-02-09 18:58:43

标签: jquery mousewheel mouse-position

我正在使用从https://github.com/brandonaaron/jquery-mousewheel/downloads下载的JQuery鼠标滚轮来获取鼠标滚轮信息。同时,我想在滚轮时获得鼠标的位置。我怎样才能做到这一点?

我目前的职能如下:

$(function() {
    $('#maindiv').mousewheel(function(event, delta, deltaX, deltaY) {   
    // somehow obtain mouse position
        // Use delta and mouse position for a purpose
    });
});

1 个答案:

答案 0 :(得分:5)

您应该能够访问任何事件的jQuery默认行为,这样您就可以阅读event.pageXevent.pageY

$(function() {
    $('#maindiv').mousewheel(function(event, delta, deltaX, deltaY) {   
        var mousePosition = { x: event.pageX, y: event.pageY };
        // ...
    });
});

请查看jQuery-Event了解详情。