Basically I've created a sliding menu using jquery.
See code below...
menu_slide = function (e) {
e.preventDefault();
if ( menuOpen == false ) {
$('.pane').animate({
'top' : $('#navigation ul').outerHeight() + 'px'
}, 250 );
menuOpen = true;
$('#top').bind( "click", menu_slide );
} else if ( menuOpen == true ) {
$('#top').unbind("click");
$('.pane').animate({
'top' : 0 + 'px'
}, 250 );
menuOpen = false;
}
}
Fired by this...
$("a.menu-button").on( "click", menu_slide );
Now when my menu opens, I would like to bind a click event to the the div#top so that div area acts as a button to close the menu. But the menu close I would like to remove it.
I thought my method of using...
$('#top').bind( "click", menu_slide );
Then removing it using...
$('#top').unbind("click");
would have the desired effect. But what happens is when I open the menu it just closes straight away.
And it's all because of this line... $('#top').bind( "click", menu_slide );
Can anyone advise on where I'm going wrong?
$.one('click', function () { ... }?$('#top').on( "click", menu_slide );- yes it does exactly the same, but it does not make sense why it closing straight away$.one(). It's a special use case for binding a single execution handler. See my answer below for details.