Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have done it so many times in so many different ways, but as I think about this question, I think the best way is to hack it with data attributes.</p> <p>Simplified example:</p> <p>HTML <em>(simplified/dirty version, added input to check focus/hover on child elements)</em></p> <pre><code>&lt;div id="container"&gt; &lt;a href="#" id="opener"&gt;open&lt;/a&gt; &lt;div id="menu"&gt; It's opened&lt;br /&gt; &lt;input /&gt;&lt;br /&gt; Here &lt;/div&gt; &lt;/div&gt; </code></pre> <p>JS *(should be optimized to avoid repeated DOM searches, but you get the<br> * <em>Updated to reflect <code>fadeOut()</code> instead of <code>hide()</code></em><br> * <em>Updated to use <code>on()</code> instead of <code>hover()</code></em></p> <pre><code>$("#menu").hide(); $("#container") .on("mouseenter", "#menu", function(){ $(this).data("hover", true); $(this).show(); }) .on("mouseleave", "#menu", function(){ $(this).data("hover", false); $(this).fadeOut(200); }); $("#container") .on("mouseenter", "#opener", function(){ $("#menu").show(); }) .on("mouseleave", "#opener", function(){ setTimeout(function(){ var menuHovered = $("#menu").data("hover"); if(!menuHovered) { $("#menu").fadeOut(200); } }, 50); }); </code></pre> <p>Fiddle</p> <p><a href="http://jsfiddle.net/Meligy/GLG6A/" rel="nofollow noreferrer">http://jsfiddle.net/Meligy/GLG6A/</a> (using <code>hover()</code> width <code>hide()</code>)<br> <a href="http://jsfiddle.net/Meligy/GLG6A/3/" rel="nofollow noreferrer">http://jsfiddle.net/Meligy/GLG6A/3/</a> (using <code>hover()</code> width <code>fadeOut()</code>, only slight change)<br> <a href="http://jsfiddle.net/Meligy/GLG6A/5/" rel="nofollow noreferrer">http://jsfiddle.net/Meligy/GLG6A/5/</a> (using <code>on()</code> on the elements instead of <code>hover()</code>)<br> <a href="http://jsfiddle.net/Meligy/GLG6A/6/" rel="nofollow noreferrer">http://jsfiddle.net/Meligy/GLG6A/6/</a> (using <code>on()</code> on the container, more realistic for AJAX) </p> <p>Credit:</p> <p>Got inspiration from here <a href="https://stackoverflow.com/questions/1273566/how-do-i-check-if-the-mouse-is-over-an-element-in-jquery/1273609#1273609">How do I check if the mouse is over an element in jQuery?</a> and thought it work best and simplest, and it actually did! (in my tests at least)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload