On my HTML5 page, I have a div with a mousemove event as follows:
$('#canvas').mousemove(function(e){ xpos = e.offsetX; ypos = e.offsetY; $('#mouse').html("X : " + xpos + " ; Y : " + ypos); });
It works great with Google Chrome. But in Firefox, both values give undefined . I checked it with Firebug, i.e. registered an e object for the console. Found that offsetX and offsetY undefined .
When I searched on Google, there was a solution that I should use layerX and layerY if both offsetX and offsetY are undefined. But from Firebug I could not find it. And even I gave him a try like this:
xpos = (e.offsetX==undefined)?e.layerX:e.offsetX; ypos = (e.offsetY==undefined)?e.layerY:e.offsetY;
But it also gives undefined as values.
I am using the latest jQuery - v1.8.2. And I am testing in my Firefox v14.0.1
Any ideas or suggestions?
EDIT
Thanks distro and vusan for helping me. The solution to the above problem is as follows:
Decision
$('#canvas').mousemove(function(e){ $('#cursor').show(); if(e.offsetX==undefined) // this works for Firefox { xpos = e.pageX-$('#canvas').offset().left; ypos = e.pageY-$('#canvas').offset().top; } else // works in Google Chrome { xpos = e.offsetX; ypos = e.offsetY; } $('#mouse').html("X : " + xpos + " ; Y : " + ypos); });
jquery html5 events undefined
Akhilesh B Chandran Oct 03 '12 at 8:35 2012-10-03 08:35
source share