0
我想检测移动时鼠标在持有者div中的位置。但是,当我将mose移动到持有者div内的SVG元素上时,它将相对于此元素而不是持有者div。任何人都知道我如何能够防止这种情况?下面看到我使用的代码:您应该使用event.currentTarget
,而不是event.target
鼠标移动时div的鼠标移动改变鼠标移动时的元素
var R = Raphael("holder", canw, canh);
$('#holder').mousemove(function (event){
if(mousemove){
position = getPosition(event);
console.log(position.x+" - "+position.y);
}
});
function getPosition(e) {
//this section is from http://www.quirksmode.org/js/events_properties.html
console.log(e);
var targ;
if (!e)
e = window.event;
if (e.target)
targ = e.target;
else if (e.srcElement)
targ = e.srcElement;
if (targ.nodeType == 3) // defeat Safari bug
targ = targ.parentNode;
console.log(targ);
// jQuery normalizes the pageX and pageY
// pageX,Y are the mouse positions relative to the document
// offset() returns the position of the element relative to the document
var x = e.pageX - $(targ).offset().left;
var y = e.pageY - $(targ).offset().top;
return {"x": x, "y": y};
};
<div id="holder"></div>
谢谢!这就是诀窍。 –