Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>EDIT</strong></p> <p>(Subsequent EDIT: added a timeout to hide after only a mouseover on the show login element, plus some other updates.)</p> <p>While I still think using <code>mouseenter</code> and <code>mouseout</code> to handle a login form is not the right way to go from a usability perspective, below is code that demonstrates what Jim Jeffers is describing and attempts to handle some of the pitfalls of the approach:</p> <pre><code>var setuplogindisplay = function(){ var $loginbox = $('#loginBox'), $loginshow = $('#loginShow'), $logincontainer = $('#loginContainer'), $cancellogin = $('#cancelLogin'), keeptimeout, closetimeout; var keepDisplay = function(){ clearAllTimeouts(); keeptimeout = setTimeout(loginHide, 2000); }; var loginDisplay = function(){ clearAllTimeouts(); if ($loginbox.is(':hidden')) { $loginbox.fadeIn(); } }; var loginHide = function(){ clearAllTimeouts(); if ($loginbox.is(':visible')) { if (!$(this).is('#cancelLogin')) { closetimeout = setTimeout(function(){ $loginbox.fadeOut(); }, 1500); } else { $loginbox.fadeOut(); } } }; function clearAllTimeouts() { if (keeptimeout) { clearTimeout(keeptimeout); } if (closetimeout) { clearTimeout(closetimeout); } } $loginshow.mouseover(loginDisplay); $loginshow.mouseout(keepDisplay); $logincontainer .mouseout(loginHide) .children() .mouseover(loginDisplay) .mouseout(keepDisplay); $cancellogin.click(loginHide); }; $(document).ready(setuplogindisplay); </code></pre> <p><a href="http://jsfiddle.net/j4Sj5/19/" rel="nofollow">http://jsfiddle.net/j4Sj5/19/</a></p> <p>Note, you have to make concessions to handle the fact <code>mouseout</code>s will fire when you mouse over elements within the <code>#logincontrol</code> element. I handle this by having them <code>loginDisplay()</code> on <code>mouseenter</code> event (it will work on <code>mouseout</code>, but it makes more logical sense on <code>mouseenter</code>).</p> <hr> <p>I would keep in mind usability of the form when trying to access it and try not to get too clever or over-engineer the user experience. Consider:</p> <pre><code>&lt;input type="button" id="cancelLogin" value="Cancel" /&gt; </code></pre> <p>Use this to close/hide the form, not an action on another element. If you put the close form action on an event like <code>mouseout</code>, you're going to aggravate your users when they move the mouse accidentally or intentionally out of the way, only to find the login form was closed when they did so. The form, IMO, should have the control which fires the event to hide it according to the user's choice.</p> <pre><code>&lt;span id="loginButton"&gt;Show Login&lt;/span&gt; &lt;div id="loginBox"&gt; &lt;label for="email_B"&gt;Email Address&lt;/label&gt; &lt;input type="text" name="email_B" id="email_B" /&gt; &lt;label for="password"&gt;Password&lt;/label&gt; &lt;input type="password_B" name="password_B" id="password_B" /&gt; &lt;input type="submit" id="login" value="Sign in" /&gt; &lt;input type="button" id="cancelLogin" value="Cancel" /&gt; &lt;label for="checkbox"&gt;&lt;input type="checkbox" id="checkbox" /&gt;Remember me&lt;/label&gt; &lt;span&gt;&lt;a href="#"&gt;Forgot your password?&lt;/a&gt;&lt;/span&gt; &lt;/div&gt; $(document).ready(function(){ var $loginbox = $('#loginBox'), $button = $('#loginButton'), $cancellogin = $('#cancelLogin'); var loginDisplay = function(){ $loginbox.fadeIn(); }; var loginHide = function(){ $loginbox.fadeOut(); }; $button.click(loginDisplay); $cancellogin.click(loginHide); }); </code></pre> <p><a href="http://jsfiddle.net/j4Sj5/4/" rel="nofollow">http://jsfiddle.net/j4Sj5/4/</a></p>
 

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