I have a square image/graph on which the user clicks.
Is there a way to display the (x,y) coordinates of the cursor to the user in real-time whenever the user hovers over the image (user does not need to click on the image)?
This should do it:
HTML
<img id="the_image" src="http://placekitten.com/200/200" />
<div id="coords"></div>
Javascript
$image = $('#the_image');
imgPos = [
$image.offset().left,
$image.offset().top,
$image.offset().left + $image.outerWidth(),
$image.offset().top + $image.outerHeight()
];
$image.mousemove(function(e){
$('#coords').html((e.pageX-imgPos[0]) +', '+ (e.pageY-imgPos[1]));
});
DEMO (updated): http://jsfiddle.net/az8Uu/2/
Note: Throttling the mousemove handler would be a good idea too, to avoid calling the function every 4 milliseconds.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With