Prevent event.click from running event.mousemove in Safari

In jQuery, the click event for an element seems to automatically fire the mousemove event for that element. Is there any way to prevent this?

Here is the simplest test case I could put together. For me, using the trackpad, clicking on the field raises the mousemove event. And I did not notice any movement in the mouse cursor.

http://jsfiddle.net/nay5d/

UPDATE. I tested the code in both Safari and Chrome and Firefox. In Safari, a click calls mousemove. In Chrome / Firefox, it seems that clicking does not trigger mousemove. Interesting.

+4
source share
4 answers

I get the same result using an outdated method in Safari. The optional mouseMove event is mouseMove on mouseUp . Example: http://jsfiddle.net/s7r8d/

 document.onmousemove = function(mouseEvent) { mouseEvent || (mouseEvent = window.event); var target = mouseEvent.target || mouseEvent.srcElement; if (target.id=="dom") { target.className = target.className=="highlight" ? "" : "highlight"; } } 

Even an attempt to force the mouseUp event was unsuccessful. We can try to report this error to Apple if it really is a Safari error.

+1
source

I just did some tests and I don’t feel that this is what is happening. If you do not register a mousemove event with an element, then no event will fire. If you are registering a mousemove event, then you should be aware that the smallest bit of mouse movement (including the movement that occurs when you click on an object) will result in the dismissal of the mousemove event.

Depending on what you are trying to execute, you may want to look into one of the many other mouse events or execute your functionality in a completely different way. (If you post what your ultimate goal for this piece of functionality is, I can try to help you with that.)

+1
source

Guy guys, I tried it like this and it seems to work, at least in chrome.

http://jsfiddle.net/ZNHDT/

 $('h1').bind('mousemove', swapClass); function swapClass() { $('h1').toggleClass('highlight'); } $('h1').mousedown(function(ev){ $("h1").unbind("mousemove"); console.log("pippo"); }) $('h1').mouseup(function(){ setTimeout(function(){ $("h1").bind("mousemove", swapClass); },10); }); 
+1
source

I think not, while your mouse is on the click area, you will also fire the mousemove event.

Here is an example: http://jsfiddle.net/marcosfromero/3KHQG/

0
source

Source: https://habr.com/ru/post/1343477/


All Articles