简体   繁体   English

获取鼠标相对于放置的div的位置

[英]Get mouse relative position to dropped div

I can get the absolute position of the mouse with: 我可以通过以下方式获取鼠标的绝对位置:

$(document).mousemove(function(e) {
        window.x = e.pageX;
        window.y = e.pageY;
    });

How can I get this position relative to a specific div? 如何获得相对于特定div的职位?

You can use the offset of the div : 您可以使用div的偏移量:

$(document).mousemove(function(e) {
    var offset = $(yourdiv).offset();
    window.x = e.pageX - offset.left;
    window.y = e.pageY - offset.top;
});

只需从div的offsetLeft和offsetTop中减去clientX和ClientY

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM