Get mouse position within div?
Possible Duplicate:
Mouse position relative to div
getting mouse position with javascript within canvas
How can I get the position of the mouse within a canvas that is a fixed size but has an automatic margin?
I can't make its position fixed and can't just use the regular mouse position on the page.
This code works perfectly:
mouseX = e.pageX - div.offsetLeft;
mouseY = e.pageY - div.offsetTop;
使用jQuery:
var divPos = {};
var offset = $("#divid").offset();
$(document).mousemove(function(e){
divPos = {
left: e.pageX - offset.left,
top: e.pageY - offset.top
};
});
使用event.layerX
和event.layerY
来获取鼠标相对于当前元素的位置:
$('#canvas').mousemove(function(e){
var mousePos = {'x': e.layerX, 'y': e.layerY};
});
Taken from jQuery site: Jquery Tutorial site
$(document).mousemove(function(e){
$('#status').html(e.pageX +', '+ e.pageY);
});
NOTE: fixed syntax
链接地址: http://www.djcxy.com/p/88550.html上一篇: 为什么MediaRecorder功能不会导致ANR错误?
下一篇: 在div中获取鼠标位置?