What is the difference between jQuery's 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).
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.
The mouseout event is sent to an element when the mouse pointer leaves the element. Any HTML element can receive this event.
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.
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/
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With