Webkit and Safari Fire mousemove, even if the mouse does not move

I read about the problems when the mousemove event is mousemove twice in Safari / Webkit, but the problem I am facing is that mousemove fires even if the mouse is not moved, That is: it fires already when the mouse cursor is over the context to which the event is attached when the page is loaded / updated. And since I am attaching it to document (full browser viewing screen), it starts right away in Safari. I tried attaching it to the html element, to body and to the wrapper of the div . Without changes.

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

Is works fine in other browsers. Does anyone see what I'm doing wrong? Thanks.

+6
javascript jquery javascript-events mousemove
source share
1 answer

I know its a dirty hack, but at least it's something: just execute the callback function a second time to mousemove .

 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 launching mousemove when the page loads - IE does this too. In any case, people using other browsers do not notice the β€œdelay”, since it is almost impossible to move the mouse only one pixel.

+1
source share

All Articles