I am creating a dropdown menu, and have an issue with opening and closing it.
When you click on link (.appHeader a.user) it opens a hidden ul (.appHeader ul.user-menu). Until now everything is working as expected. But when i re-click the same link (with the ul open), it closes it and re-opens it.
Because i have an animation on the ul show (fadeIn) it creates a very nasty animation loop if you keep clicking it several times fast.
HTML:
<div class="appHeader">
<div>
<a class="user" href="#">Welcome, <span>Name Lastname</span></a>
<ul class="user-menu">
<div></div>
<li><a href="#">Menu item</a></li>
<li><a href="#">Menu item</a></li>
<li><a href="#">Menu item</a></li>
<li><a href="#">Menu item</a></li>
</ul>
</div>
</div>
CSS:
.appHeader ul.user-menu {
display: none;
position:absolute;
z-index: 900;
width: 150px;
border: 1px solid #111111;
background: #FFFFFF;
box-shadow: 1px 1px 1px 0px rgba(0, 0, 0, 0.2);
text-align: left;
list-style: none;
padding: 10px 0;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
-ms-border-radius: 4px;
-o-border-radius: 4px;
border-radius: 4px;
}
.appHeader ul.user-menu li {
margin:0;
padding:0 10px;
display: block;
}
.appHeader ul.user-menu li a {
height: 15px;
text-transform: none;
background: #FFFFFF;
line-height: 1em;
font-size: 1em;
display: block;
text-decoration: none;
color: #222222;
padding:0;
padding: 8px 10px;
outline: none;
}
.appHeader ul.user-menu li a:hover {
background: #f2f2f2;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
-ms-border-radius: 3px;
-o-border-radius: 3px;
border-radius: 3px;
}
.appHeader ul.user-menu li a:active {
background: #CCCCCC;
color: #111111;
}
.appHeader ul.user-menu div {
height: 8px;
width: 16px;
background: #FFF;
margin:-18px 0 10px 120px;
background: url(data:image/png;base64,...) no-repeat;
}
JS:
$('.appHeader a.user').on('click', function() {
$('.appHeader ul.user-menu').fadeIn('fast');
$(document).mouseup(function (e) {
var container = $('.appHeader ul.user-menu');
if (!container.is(e.target) && container.has(e.target).length === 0) {
$('.appHeader ul.user-menu').fadeOut('fast');
}
});
});
Here is the JSFiddle for the problem http://jsfiddle.net/MxfPq/4/ (try clicking the name fast several times)
Is there any way i can disable the click while the ul is shown? and then re-enabling the click when the ul is hidden ?
I have tried with the .one() jQuery function, but this is not the functionality i want, this disables the second click entirely.