Basically I unbind the mouseleave after an element is clicked. But I want to have the mouseleave-event for this element working again when another element is clicked. My code is working, but it feels bulky, because I repeat the animation on and on in my code.
Is there no other method to supress the mouseleave temporarily other than unbinding and “re-binding” ? Any suggestions ?
HTML
<div class="container">
CONTAINER ONE
</div>
<div class="container">
CONTAINER TWO
</div>
JS:
$(document).ready(function()
{
//the default hover-event
$('.container').hover(
function(){
$(this).stop().animate({'padding-bottom':'10px'},{queue:false,duration:160});
},
function() {
$(this).stop().animate({'padding-bottom':'0px'},{queue:false,duration:160});
}
);
$('.container').click(function(e) {
e.preventDefault();
// enables the mouseleave for all other `.container`s again.
// also bring the old clicked element into unhovered position again
$('.container')
.bind('mouseleave',function() {
$(this).stop().animate({'padding-bottom':'0px'},{queue:false,duration:160});
}).not(this).stop().animate({'padding-bottom':'0px'},{queue:false,duration:160});
// supress the mouseleave for the clicked element
$(this).unbind('mouseleave');
})
});
This may be a better way (example):