I create table, handlers hooked up mousemove.
But in top left point I get .offsetX.offsetY equals -5-5.
Why? I need 0\0.
<table cellpadding="0"
id="target"
cellspacing="0"
width="602"
height="500"
style="float:left;
position:relative;
background: url(/content/games/kamikaze2/back.jpg) no-repeat 0 0;">
<tbody>...
</tbody>
</table>
<script type="text/javascript">
$("#target").mousemove(function (event) {
var msg = "Handler for .mousemove() called at ";
msg += event.offsetX + ", " + event.offsetY;
$("#log").append("<div>" + msg + "</div>");
});
</script>
More examples here: show-popup-on-mouse-location
Mouse coordinates within element
The most accurate way to get the mouse coordinates within an element (without scrollbars) relative to viewport is by calculating the difference between
Subtracting those values you can retrieve the exact mouse position, x:0, y:0 being the upper left corner of your element. Even if an element is CSS-transformed, i.e:
transform: translate(-50%, -50%), the returned values are still correct.