58

In my HTML5 page, I have a div with mousemove event as follows:

$('#canvas').mousemove(function(e){
    xpos = e.offsetX;
    ypos = e.offsetY;
    $('#mouse').html("X : " + xpos + " ; Y : " + ypos);
});

It works fine with Google Chrome. But in Firefox, both the are giving the value undefined. I have checked it by using Firebug, that is, logged the e object to console. Both offsetX and offsetY are found to be undefined.

When I searched in Google, there was a solution saying I should use layerX and layerY, if both offsetX and offsetY are undefined. But from Firebug, I was not able to find it. And even I had given it a try like this:

xpos = (e.offsetX==undefined)?e.layerX:e.offsetX;
ypos = (e.offsetY==undefined)?e.layerY:e.offsetY;

But that's also giving undefined as values.

I am using the most recent jQuery - v1.8.2. And I am testing in my Firefox v14.0.1

Any ideas or suggestions?


EDIT

Thanks to dystroy and vusan for helping me. The solution to the above issue is as follows:

SOLUTION

$('#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);
});
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Akhilesh B Chandran
  • 6,523
  • 7
  • 28
  • 55

5 Answers5

20

Try using layerX and layerY for Firefox and offsetX for other browser.

If event fired with jquery:

xpos = e.offsetX === undefined ? e.originalEvent.layerX : e.offsetX;
ypos = e.offsetY === undefined ? e.originalEvent.layerY : e.offsetY;

If event fired with javascript:

xpos = e.offsetX==undefined?e.layerX:e.offsetX;
ypos = e.offsetY==undefined?e.layerY:e.offsetY;
vusan
  • 5,221
  • 4
  • 46
  • 81
  • 2
    Thanks.. But what I wanted is relative to a container(`#canvas`). pageX/Y are relative to the `` element - http://stackoverflow.com/questions/6073505/what-is-the-difference-between-screenx-y-clientx-y-and-pagex-y – Akhilesh B Chandran Oct 03 '12 at 08:51
  • 8
    Use `var xInCanvas = e.pageX-$canvas.offset().left;` – Denys Séguret Oct 03 '12 at 08:58
  • 4
    Thanks guys. I got it working when I tried what @dystroy had suggested. The code is as follows: `if(e.offsetX==undefined) { xpos = e.pageX-$('#canvas').offset().left; ypos = e.pageY-$('#canvas').offset().top; } else { xpos = e.offsetX; ypos = e.offsetY; }` Thanks again guys :) – Akhilesh B Chandran Oct 03 '12 at 09:12
  • Check out [this answer](http://stackoverflow.com/a/14841138/24874) for a solution that doesn't require jQuery. – Drew Noakes Sep 29 '13 at 22:22
  • 1
    Although pageX and pageY work they do not answer the question directly. http://stackoverflow.com/a/20397642/11792 – Pavel Nikolov Dec 05 '13 at 10:36
  • pageX is not the same as offsetX – pronebird Dec 11 '14 at 14:10
  • it's `e.originalEvent.layerX` not `e.layerX`, see other answer(s). And yet no reason to have the same answer 3 times :) – huysentruitw Dec 15 '14 at 15:19
  • This answer doesn't work anymore because `e.offsetX` is always `0` and not `undefined` in Firefox. – Take Nov 25 '21 at 03:17
20

Use layerX and layerY in FF and offsetX and offsetY in all other browsers.

$('#canvas').mousemove(function(e){
  xpos = e.offsetX === undefined ? e.originalEvent.layerX : e.offsetX;
  ypos = e.offsetY === undefined ? e.originalEvent.layerY : e.offsetY;

  $('#mouse').html("X : " + xpos + " ; Y : " + ypos);
});
Pavel Nikolov
  • 9,401
  • 5
  • 43
  • 55
  • 1
    This and the other answer specifying originalEvent need more upvotes. The "best answer" does provide a solution but these are more accurate as layerX and layerY are more equivalent to offsetX and offsetY. Thanks :) – James Hill Jul 31 '14 at 21:53
14

You did not find them because its in the originalEvent. try: e.originalEvent.layerX e.originalEvent.layerY

About the pageX and pageY they are not calculating the same thing. layerX is the left of the object from the first relative/absolute parent. pageX is the left of the object from the page.

Roy Shoa
  • 3,117
  • 1
  • 37
  • 41
0

This works fine in firefox and others.

var offsetRequired = (e.offsetX || e.pageX - $(e.target).offset().left);
Stark Buttowski
  • 1,799
  • 2
  • 10
  • 21
0

Firefox actually does support MouseEvent.offsetX and MouseEvent.offsetY after release 39.0, which is released in july 2015.

Memet Olsen
  • 4,578
  • 5
  • 40
  • 50
  • But the values for offsetX/Y in Firefox are not the same as they are in Chrome. Need to convert them but don't know how. – scott Jul 12 '16 at 23:11
  • I'm getting the same values with the newest version of both browsers. – Memet Olsen Jul 13 '16 at 07:02
  • I have Chrome 49 because my Mac OS is 10.6.8. I can't use a newer version of Chrome until I update my OS (or my boss buys me a new machine). – scott Jul 13 '16 at 21:34