Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1084026 of mouseleave

  • Revision slug: Web/Events/mouseleave
  • Revision title: mouseleave
  • Revision id: 1084026
  • Created:
  • Creator: paul.irish
  • Is current revision? No
  • Comment mouseleave has fewer perf concerns than mouseout. i can't find a way to see mouseout being a better option for perf. teoli, holler at me if you think im crazy. :)

Revision Content

The mouseleave event is fired when a pointing device (usually a mouse) is moved off the element that has the listener attached.

Similar to {{event('mouseout')}}, it differs in that it doesn't bubble and that it isn't sent until the pointer has moved from its physical space and the one of all its descendants.

mouseenter.png mouseover.png
One mouseenter event is sent to each element of the hierarchy when entering them. Here 4 events are sent to the four elements of the hierarchy when the pointer reaches the text. One single mouseover event is sent to the deepest element of the DOM tree, then it bubbles up the hierarchy until it is canceled by a handler or reaches the root.

Combined with the behavior of its symmetrical event, {{domeventxref('mouseenter')}}, the mouseleave DOM Event acts in a very similar way to the CSS {{cssxref(':hover')}} pseudo-class.

  • Interface :{{domxref('MouseEvent')}}
  • Synchronicity :synchronous
  • Bubbles : no
  • Target : {{cssxref('Element')}}
  • Cancelable : no
  • Default action : none

General info

Specification
DOM L3
Interface
MouseEvent
Bubbles
No
Cancelable
No
Target
Element
Default Action
None

Properties

{{OpenEventProperties}}{{UIEventProperties}}{{MouseEventProperties}}{{CloseEventProperties}}

Examples

The mouseout documentation has an example illustrating the difference between mouseout and mouseleave.

The following example illustrates how to use mouseout to simulate the principle of event delegation for the mouseleave event.

<ul id="test">
  <li>
    <ul class="leave-sensitive">
      <li>item 1-1</li>
      <li>item 1-2</li>
    </ul>
  </li>
  <li>
    <ul class="leave-sensitive">
      <li>item 2-1</li>
      <li>item 2-2</li>
    </ul>
  </li>
</ul>

<script>
  var delegationSelector = ".leave-sensitive";

  document.getElementById("test").addEventListener("mouseout", function( event ) {
    var target = event.target,
        related = event.relatedTarget,
        match;

    // search for a parent node matching the delegation selector
    while ( target && target != document && !( match = matches( target, delegationSelector ) ) ) {
        target = target.parentNode;
    }

    // exit if no matching node has been found
    if ( !match ) { return; }

    // loop through the parent of the related target to make sure that it's not a child of the target
    while ( related && related != target && related != document ) {
        related = related.parentNode;
    }

    // exit if this is the case
    if ( related == target ) { return; }

    // the "delegated mouseleave" handler can now be executed
    // change the color of the text
    target.style.color = "orange";
    // reset the color after a small amount of time
    setTimeout(function() {
        target.style.color = "";
    }, 500);
    
    
  }, false);
 
 
  // function used to check if a DOM element matches a given selector
  // the following code can be replaced by this IE8 compatible function: https://gist.github.com/2851541
  function matches( elem, selector ){
    // the matchesSelector is prefixed in most (if not all) browsers
    return elem.matchesSelector( selector );
  };
</script>

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 30[1] 10[2] 5.5 {{CompatVersionUnknown}}
{{CompatNo}} 15.0
17.0
7[3]
On disabled form elements {{CompatNo}} {{CompatGeckoDesktop("44.0")}}[4] {{CompatNo}} {{CompatNo}} {{CompatUnknown}}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
On disabled form elements {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}

[1] Implemented in bug 236215.

[2] Implemented in {{bug("432698")}}.

[3] Safari 7 fires the event in many situations where it's not allowed to, making the whole event useless. See bug 470258 for the description of the bug (it existed in old Chrome versions as well). Safari 8 has correct behavior

[4] Implemented in {{bug("218093")}}.

See also

{{MouseRelatedEvents}}

Revision Source

<p>The <code>mouseleave</code> event is fired when a pointing device (usually a mouse) is moved off the element that has the listener attached.</p>

<p>Similar to {{event('mouseout')}}, it differs in that it doesn't <a href="/en-US/docs/DOM/event.bubbles">bubble</a> and that it isn't sent until the pointer has moved from its physical space and the one of all its descendants.</p>

<table class="standard" style="border:solid 1px">
 <tbody>
  <tr>
   <td><img alt="mouseenter.png" class="default internal" src="/@api/deki/files/5910/=mouseleave.png" /></td>
   <td><img alt="mouseover.png" class="default internal" src="/@api/deki/files/5911/=mouseout.png" /></td>
  </tr>
  <tr>
   <td>One <code>mouseenter</code> event is sent to each element of the hierarchy when entering them. Here 4 events are sent to the four elements of the hierarchy when the pointer reaches the text.</td>
   <td>One single <code>mouseover</code> event is sent to the deepest element of the DOM tree, then it bubbles up the hierarchy until it is canceled by a handler or reaches the root.</td>
  </tr>
 </tbody>
</table>

<p>Combined with the behavior of its symmetrical event, {{domeventxref('mouseenter')}}, the <code>mouseleave</code> DOM Event acts in a very similar way to the CSS {{cssxref(':hover')}} pseudo-class.</p>

<ul style="display:table;padding: 0;border-left: 2px solid;margin-left:0.5em;">
 <li style="display:table-row;padding: 3px;margin:0;"><dfn style="display:table-cell;padding: 0 5px;border-bottom: none;cursor:inherit;">Interface :</dfn>{{domxref('MouseEvent')}}</li>
 <li style="display:table-row;padding: 3px;margin:0;"><dfn style="display:table-cell;padding: 0 5px;border-bottom: none;cursor:inherit;">Synchronicity :</dfn>synchronous</li>
 <li style="display:table-row;padding: 3px;margin:0;"><dfn style="display:table-cell;padding: 0 5px;border-bottom: none;cursor:inherit;">Bubbles :</dfn> no</li>
 <li style="display:table-row;padding: 3px;margin:0;"><dfn style="display:table-cell;padding: 0 5px;border-bottom: none;cursor:inherit;">Target :</dfn> {{cssxref('Element')}}</li>
 <li style="display:table-row;padding: 3px;margin:0;"><dfn style="display:table-cell;padding: 0 5px;border-bottom: none;cursor:inherit;">Cancelable :</dfn> no</li>
 <li style="display:table-row;padding: 3px;margin:0;"><dfn style="display:table-cell;padding: 0 5px;border-bottom: none;cursor:inherit;">Default action :</dfn> none</li>
</ul>

<h2 id="General_info">General info</h2>

<dl>
 <dt style="clear: left; float: left; text-align: right; width: 120px;">Specification</dt>
 <dd style="margin: 0 0 0 120px;"><a class="external" href="https://www.w3.org/TR/DOM-Level-3-Events/#event-type-mouseleave">DOM L3</a></dd>
 <dt style="clear: left; float: left; text-align: right; width: 120px;">Interface</dt>
 <dd style="margin: 0 0 0 120px;">MouseEvent</dd>
 <dt style="clear: left; float: left; text-align: right; width: 120px;">Bubbles</dt>
 <dd style="margin: 0 0 0 120px;">No</dd>
 <dt style="clear: left; float: left; text-align: right; width: 120px;">Cancelable</dt>
 <dd style="margin: 0 0 0 120px;">No</dd>
 <dt style="clear: left; float: left; text-align: right; width: 120px;">Target</dt>
 <dd style="margin: 0 0 0 120px;">Element</dd>
 <dt style="clear: left; float: left; text-align: right; width: 120px;">Default Action</dt>
 <dd style="margin: 0 0 0 120px;">None</dd>
</dl>

<h2 id="Properties">Properties</h2>

<p>{{OpenEventProperties}}{{UIEventProperties}}{{MouseEventProperties}}{{CloseEventProperties}}</p>

<h2 id="Examples">Examples</h2>

<p>The <a href="https://developer.mozilla.org/en-US/docs/Web/Events/mouseout#Example"><code>mouseout</code> </a>documentation has an example illustrating the difference between <code>mouseout</code> and <code>mouseleave</code>.</p>

<p>The following example illustrates how to use <code>mouseout</code> to simulate the principle of event delegation for the mouseleave event.</p>

<pre class="brush: html">
&lt;ul id="test"&gt;
  &lt;li&gt;
    &lt;ul class="leave-sensitive"&gt;
      &lt;li&gt;item 1-1&lt;/li&gt;
      &lt;li&gt;item 1-2&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;ul class="leave-sensitive"&gt;
      &lt;li&gt;item 2-1&lt;/li&gt;
      &lt;li&gt;item 2-2&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;script&gt;
  var delegationSelector = ".leave-sensitive";

  document.getElementById("test").addEventListener("mouseout", function( event ) {
    var target = event.target,
        related = event.relatedTarget,
        match;

    // search for a parent node matching the delegation selector
    while ( target &amp;&amp; target != document &amp;&amp; !( match = matches( target, delegationSelector ) ) ) {
        target = target.parentNode;
    }

    // exit if no matching node has been found
    if ( !match ) { return; }

    // loop through the parent of the related target to make sure that it's not a child of the target
    while ( related &amp;&amp; related != target &amp;&amp; related != document ) {
        related = related.parentNode;
    }

    // exit if this is the case
    if ( related == target ) { return; }

    // the "delegated mouseleave" handler can now be executed
    // change the color of the text
    target.style.color = "orange";
    // reset the color after a small amount of time
    setTimeout(function() {
        target.style.color = "";
    }, 500);
    
    
  }, false);
 
 
  // function used to check if a DOM element matches a given selector
  // the following code can be replaced by this IE8 compatible function: https://gist.github.com/2851541
  function matches( elem, selector ){
    // the matchesSelector is prefixed in most (if not all) browsers
    return elem.matchesSelector( selector );
  };
&lt;/script&gt;</pre>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>30<sup>[1]</sup></td>
   <td>10<sup>[2]</sup></td>
   <td>5.5</td>
   <td>{{CompatVersionUnknown}}<br />
    {{CompatNo}} 15.0<br />
    17.0</td>
   <td>7<sup>[3]</sup></td>
  </tr>
  <tr>
   <td>On disabled form elements</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatGeckoDesktop("44.0")}}<sup>[4]</sup></td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td>On disabled form elements</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<p>[1] Implemented in <a href="https://crbug.com/236215">bug 236215</a>.</p>

<p>[2] Implemented in {{bug("432698")}}.</p>

<p>[3] Safari 7 fires the event in many situations where it's not allowed to, making the whole event useless. See <a href="https://crbug.com/470258">bug 470258</a> for the description of the bug (it existed in old Chrome versions as well). Safari 8 has correct behavior</p>

<p>[4] Implemented in {{bug("218093")}}.</p>

<h2 id="See_also">See also</h2>

<p>{{MouseRelatedEvents}}</p>
Revert to this revision