2012-05-03 44 views
3

我有一个DIV,我可以使用.offset()获得偏移量。如何计算与DIV位置相关的鼠标坐标

但我想获得与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). 

请帮我解决这个问题。我如何做到这一点。

回答

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

它的工作对我来说..谢谢 – Exception