Mouse Position by event.positionX & event.positionY using JQUERYWe can get the position of the mouse from left side and from top edge of the screen by using event.positionX and event.postionY
Now let us try to develop code to display mouse position from left and top edge of the screen. We will keep this code inside a click event so once the user cliks any where in the page, the position of the Mouse is displayed.
Here is the code …
Here we have seen how the position is displayed on mouse click, same display we can attach to mousemove event so when we move the mouse the position is changed continuously.
In above examples we have seen position of mouse with respect to document. Now let us calculate position with respect to any other element. In our demo we have used HTML5 Canvas but you can use any other object or image. In this demo we still read the mouse position with respect to document but adjust the value by taking offset of the element. We can read the offset object like this
var offset = $(this).offset();Based on this value we can calculate position of the mouse.