Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between jQuery's mouseout() and mouseleave()?

What is the difference between jQuery's mouseout() and mouseleave()?

like image 861
Dave Avatar asked Nov 23 '10 16:11

Dave


People also ask

What is the difference between Mouseout and Mouseleave?

This means that mouseleave is fired when the pointer has exited the element and all of its descendants, whereas mouseout is fired when the pointer leaves the element or leaves one of the element's descendants (even if the pointer is still within the element).

What is the difference between mouseover and Mouseenter?

The mouseover event triggers when the mouse pointer enters the div element, and its child elements. The mouseenter event is only triggered when the mouse pointer enters the div element. The onmousemove event triggers every time the mouse pointer is moved over the div element.

What is Mouseout event in jquery?

The mouseout event is sent to an element when the mouse pointer leaves the element. Any HTML element can receive this event.

What is Mouseout?

The mouseout event is fired at an Element when a pointing device (usually a mouse) is used to move the cursor so that it is no longer contained within the element or one of its children.


2 Answers

The mouseleave event differs from mouseout in the way it handles event bubbling. If mouseout were used in this example, then when the mouse pointer moved out of the Inner element, the handler would be triggered. This is usually undesirable behavior. The mouseleave event, on the other hand, only triggers its handler when the mouse leaves the element it is bound to, not a descendant. So in this example, the handler is triggered when the mouse leaves the Outer element, but not the Inner element.

Source: http://api.jquery.com/mouseleave/

like image 60
meder omuraliev Avatar answered Sep 19 '22 18:09

meder omuraliev


There can be times when mouseout is a better choice than mouseleave.

For example, let's say you've created a tooltip that you want displayed next to an element on mouseenter. You use setTimeout to prevent the tooltip from popping up instantly. You clear the timeout on mouseleave using clearTimeout so if the mouse leaves the tooltip won't be displayed. This will work 99% of the time.

But now let's say the element you have a tooltip attached to is a button with a click event, and let's also assume this button prompts the user with either a confirm or alert box. The user clicks the button and the alert fires. The user pressed it fast enough that your tooltip didn't have a chance to pop up (so far so good).

The user presses the alert box OK button, and the mouse leaves the element. But since the browser page is now in a locked state, no javascript will fire until the OK button has been pressed, meaning your mouseleave event WILL NOT FIRE. After the user presses OK the tooltip will popup (which is not what you wanted).

Using mouseout in this case would be the appropriate solution because it will fire.

like image 42
Darcy Avatar answered Sep 18 '22 18:09

Darcy