mouseenter

  • Revision slug: Mozilla_event_reference/mouseenter
  • Revision title: mouseenter
  • Revision id: 239543
  • Created:
  • Creator: louisremi
  • Is current revision? No
  • Comment

Revision Content

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

General info

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

Properties

{{ MouseEventProperties() }}

Examples

The mouseover documentation has an example illustrating the difference between mouseover and mouseenter.

The following example illustrates how to use mouseover to simulate the principle of event delegation for the mouseenter event.

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

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

  document.getElementById("test").addEventListener("mouseover", 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 mouseenter" 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>

Revision Source

<p>The <code>mouseenter</code> event is fired when a pointing device (usually a mouse) is moved over the element that has the listener attached.</p>
<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="http://www.w3.org/TR/DOM-Level-3-Events/#event-type-mouseenter" title="http://www.w3.org/TR/DOM-Level-3-Events/#event-type-mouseenter">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;">
    Synchronicity</dt>
  <dd style="margin: 0 0 0 120px;">
    synchronous</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;">
    Yes</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>{{ MouseEventProperties() }}</p>
<h2 id="Examples">Examples</h2>
<p>The <a href="/en/Mozilla_event_reference/mouseover#Example" title="en/Mozilla_event_reference/mouseover#Example"><code>mouseover</code> </a>documentation has an example illustrating the difference between <code>mouseover</code> and <code>mouseenter</code>.</p>
<p>The following example illustrates how to use <code>mouseover</code> to simulate the principle of event delegation for the mouseenter event.</p>
<pre class="brush:html;">&lt;ul id="test"&gt;
  &lt;li&gt;
    &lt;ul class="enter-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="enter-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 = ".enter-sensitive";

  document.getElementById("test").addEventListener("mouseover", 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 mouseenter" 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>
Revert to this revision