I try to get mouse position relative to an element using mousemove. It works fine, but when the mouse hover a child element, the coordinates are relative to this child. I want the mouse position relative to the parent div, not the child.
See this JSFiddle for example.
var object = document.getElementsByClassName('object'),
scene = document.getElementById('scene');
function initMove() {
for(var i=0; i<object.length; i++) {
object[i].addEventListener("mousemove", function(event) {
//event.stopPropagation();
return false;
}, false);
}
scene.addEventListener("mousemove", function (event) {
//event.stopPropagation();
//event.currentTarget;
moveX = event.offsetX;
moveY = event.offsetY;
console.log(moveX + ' + ' + moveY);
}, false);
}
function init() {
initMove();
document.onselectstart = function(){ return false; }
};
init();
Adding event.stopPropagation()
on child return no data.
And I'm not sure how event.currentTarget
works.
I can't use mouseenter or every other mouse only controls, because I want this to be touch friendly (by replacing mousemove by touchmove).
Any ideas?