In my HTML5 page, I have a div with mousemove
event as follows:
$(\'#canvas\').mousemove(function(e){
xpos = e.offsetX;
ypos = e.offsetY
This works fine in firefox and others.
var offsetRequired = (e.offsetX || e.pageX - $(e.target).offset().left);
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;
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);
});
Firefox actually does support MouseEvent.offsetX
and MouseEvent.offsetY
after release 39.0, which is released in july 2015.
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.