Sujit Agarwal
Sujit Agarwal

Reputation: 12508

How to detect co-ordinates of mouse pointer over a div

Suppose there is a DIV with the following properties:

top:100;
left:100;
height:200;
width:200;

Now, when i position the mouse pointer in the browser at co-ords(105,210), it means that the mouse pointer is on the div at (5, 110). So this is the thing that i want to calculate using jQuery. Can anyone help me with some code suggestions?

Upvotes: 0

Views: 248

Answers (1)

anroesti
anroesti

Reputation: 11373

Just subtract the properties offsetLeft of your DIV from pageX and offsetTop from pageY.

$(document).mousemove( function(e) {
    var x = e.pageX - $('#example').offsetLeft;
    var y = e.pageY - $('#example').offsetTop;
}

Upvotes: 1

Related Questions