Grab mouse coordinates only when clicking on the screen

Asked

Viewed 423 times

1

I have a problem where I need to get the mouse position only when I click on the div, that is, to get the position where the mouse was clicked inside the div. I can already get the position of the mouse, with the event "Mousemove", but I still can’t get the position of the click, in my code, it enters first in the click, but does not take the event inside the click.

m_This.HtmlObject.click(function () {
      $(document).on("mousemove", function (event) {                
            var positionX = event.pageX;
            var positiony = event.pageY;
      }
}

1 answer

0


You can use the event click instead of mousemove.

var lastClickPosition;

document.addEventListener('click', storePosition, true);
function storePosition(e) {
  lastClickPosition = { x: e.pageX, y: e.pageY };
  console.log(lastClickPosition);
}
<p>Clique na página</p>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.