0

I am currently dragging an element with my mouse, I get the X and Y of the element then subtract half of the height/width:

const newX = X - (width / 2);
const newY = Y - (height / 2);

But I need a way of getting the coordinates of the mouse click inside the element so that the point of dragging isn't the centre of the element, it's wherever the element was first clicked.

How can I do this?

Thanks.

JmJ
  • 1,989
  • 3
  • 29
  • 51

1 Answers1

0

In your click, mousedown, mouseup or mousemove event you can check the mouseX and mouseY coordinates. They are relative to the page, not the element.

Here you have a practical site where you can test it in real time: http://www.w3schools.com/jsref/event_clientx.asp

If you want it relative to the element, you can check this answer: https://stackoverflow.com/a/10429969/1525495

Community
  • 1
  • 1
Jorge Fuentes González
  • 11,568
  • 4
  • 44
  • 64