Mouse Position by event.positionX & event.positionY using JQUERY

We 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
$(document).ready(function() {

$("#t1").html('You  clicked at X position: '+event.pageX + " , Y position: "+event.pageY);

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.


* indicates required
Subscribe to plus2net

    Post your comments , suggestion , error , requirements etc here .

    Most Popular JQuery Scripts


    Two dependant list boxes


    Calendar with Date Selection


    Data change by Slider


    Show & Hide element

    JQuery Video Tutorials

    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 All rights reserved worldwide Privacy Policy Disclaimer