繁体   English   中英

如何计算与DIV位置相关的鼠标坐标

[英]How to calculate mouse coordinates related to DIV position

我有一个DIV,我可以使用.offset()获得偏移量。

但我试图获得与div相关的鼠标位置。 当我将鼠标悬停在DIV上时,我可以获得鼠标的x和y偏移量。 但这些将与Document相关。 但它应该以下面的方式计算。

 For example DIV dimensions are 200 and 200.
 then it should calculate offsets related to (0,200)(200,0),(200,200),(200,200).

请帮帮我。 我怎么能这样做

你的意思是:

$('#someele').click(function(e) {
  var offset = $(this).offset();
  var x = Math.floor(e.pageX - offset.left);
  var y = Math.floor(e.pageY - offset.top);
  console.log('x pos:' +  x  + ' y pos:' + y);
});

暂无
暂无

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

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