我想在鼠标移动时检测鼠标在保持器 div 中的位置。但是,当我将鼠标移到持有人 div 内的 SVG 元素上时,它将采用与此元素而不是持有人 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>