.mouseleave() methodThe .mouseleave() method in jQuery is used to run a function when the mouse pointer leaves an elements, or trigger the mouseleave event on an element.
When the mouse pointer leaves the element, the event mouseleave is fired.
.mouseleave() method Syntax.mouseleave() .mouseleave(handler) .mouseleave(eventData,handler)
| Parameter | Type | Description |
|---|---|---|
handler |
Function | A function to execute when the mouseleave is triggered on the matched element. |
eventData |
Anything | Additional data passed to the event handler. |
.mouseleave(handler) and .mouseleave(eventData,handler) are shortcut for .on( "mouseleave", handler )
.mouseleave() is shortcut for .trigger( "mouseleave" )
mouseleave is relative to mouseenter.mouseleave() method ExampleTrigger the mouseleave event on target:
HTML
<div id="target">target</div> <div id="btn"> Trigger mouseleave event on target </div>
jQuery
$( "#target" ).mouseleave(function() {
console.log( "mouseleave." );
});
$("#btn").click(function(){
$( "#target" ).mouseleave();
}
.mouseleave([eventData,]handler) method ExampleFollowing is a simple example which change the element's background color when the mouse pointer enters or leaves the element.
$("p").mouseleave(function(){
$(this).css("background-color","cyan");
});
.mouseleave() VS .mouseover()The difference between .mouseleave() and .mouseout() is:
mouseout fires when the pointer moves out the child element as well.mouseleave fires only when the pointer moves out of the bound element.