Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何计算与DIV位置相关的鼠标坐标_Javascript_Jquery - Fatal编程技术网

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

Javascript 如何计算与DIV位置相关的鼠标坐标,javascript,jquery,Javascript,Jquery,我有一个DIV,可以使用.offset()获得偏移量 但是我试图得到与div相关的鼠标位置。当我悬停div时,我可以得到鼠标的x和y偏移。但这些将根据文件进行计算。但应按以下方式计算 For example DIV dimensions are 200 and 200. then it should calculate offsets related to (0,200)(200,0),(200,200),(200,200). 请帮我做这个。我怎样才能做到这一点。你的意思是: $('#so

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

但是我试图得到与div相关的鼠标位置。当我悬停div时,我可以得到鼠标的x和y偏移。但这些将根据文件进行计算。但应按以下方式计算

 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);
});