Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webkit and Safari fire mousemove even when mouse doesn't move

I've read about issues where the mousemove event is fired twice in Safari/Webkit, but the problem I'm facing is that mousemove fires even when the mouse is not moved. That is: it already fires when the mouse cursor is positioned above the context that the event is attached to when the page is loaded/refreshed. And because I'm attaching it to document (entire viewport of the browser), it fires right away in Safari. I've tried to attach it to the html element, to the body and to a wrapper div. No change.

$(document).bind('mousemove', function() {
  alert('Mouse moved!');
  $(document).unbind('mousemove');
});

Is does work ok in other browsers. Anyone seeing what I'm doing wrong? Thanks.

like image 890
Roel Avatar asked Apr 10 '10 09:04

Roel


People also ask

Does Mousemove event bubble?

That means that if you stop the propagation for the onmousemove event when you start dragging, it will never bubble up to the document node and in turn will result in the draggable div never being dragged untill you actually move the mouse outside the "inner" div area.

What is Mousemove?

The mousemove event is fired at an element when a pointing device (usually a mouse) is moved while the cursor's hotspot is inside it.

Which of the following event get called when mouse is just moved over the element?

The mouseover event occurs when a mouse pointer comes over an element, and mouseout – when it leaves.

Which are the simplest mouse event?

click: the simplest event. dblclick: fired on a double click on an HTML element. mousedown: fired when the button is pressed. mouseup: fired when the button is released.


1 Answers

I know it’s a dirty hack, but at least this is something: simply execute the callback function the second time mousemove is fired.

var $document = $(document), i = 0;
$document.bind('mousemove', function() {
 if (++i > 1) {
  alert('Mouse moved!');
  $document.unbind('mousemove');
 };
});

Demo: http://fiddle.jshell.net/Yz5Bd/show/light/

For what it’s worth, Safari is not the only browser firing mousemove on page load — IE does it too. Anyhow, people using other browsers won’t notice the ‘delay’, since it’s virtually impossible to move your mouse only one pixel.

like image 156
Mathias Bynens Avatar answered Oct 01 '22 22:10

Mathias Bynens