I have a link which is a persons username. When you click it, a little flyout window (an unordered list) appears. I’m trying to make it so:
- Clicking on the persons username toggles the flyout ul with fadein/out
- If you click to show the flyout, and click ON the ul, it doesnt disappear
- if you click on the body anywhere except the flyout, it disappears
This is what I have so far but I’m basically just fooling myself into thinking I know what I’m doing 🙁
$(document).ready(function() {
$('.flyout h3 a').click(function() {
var flyout = $('.flyout ul');
flyout.fadeToggle(80,function() {
if ( flyout.is(':visible') ) {
console.log('visible');
$(document).on('click',function(e) {
flyout.fadeOut(80);
e.stopPropagation();
});
} else {
$(document).off('click');
}
});
});
});
html:
<div class="flyout">
<h3>Welcome back, <a href="#">Dogbreath</a><img src="img/down-arrow.png" alt="dropdown"></h3>
<div class="menu">
<ul>
<li><a href="#"><span class="icon">👥</span>Users</a></li>
<li><a href="#"><span class="icon">🕨</span>Groups</a></li>
<li><a href="#"><span class="icon">⚙</span>Configuration</a></li>
<li><a href="#"><span class="icon"></span>Logout</a></li>
</ul>
</div>
</div>
you are trying to do too much in the callback function for
fadeToggle()try just having something like this (untested)
this takes care of hiding/showing the menu when the username is clicked, no?
then, separately and independently, bind events on
document(this might take some tweaking)