I am trying to get the mouse position relative to an element using mousemove. It works fine, but when the mouse moves over a child, the coordinates are relative to that 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() for the child does not return data. And I'm not sure how event.currentTarget works.
I cannot use mouseenter or any other mouse-only controls because I want it to be convenient for contact (by replacing mousemove with touchmove).
Any ideas?
Julian
source share