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

Exc*_*ion 3 javascript jquery

我有一个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).
Run Code Online (Sandbox Code Playgroud)

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

Sud*_*oti 6

你的意思是:

$('#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);
});
Run Code Online (Sandbox Code Playgroud)