Sunday, 15 April 2012

onclick - jQuery Slide in on click, slideOut on click with addclass not working? -



onclick - jQuery Slide in on click, slideOut on click with addclass not working? -

i attempting create slide out menu has button slide in/out classed 'open'.

when open button clicked jquery slide in , adds class 'close' , removes class 'open' - works fine.

then when 'close' clicked attempting slide out, add together class 'open' , remove class 'close' take it's original state.

but close not work, adds close class close function not working on click, maybe can't have function on class has been added...?

any help much appreciated.

$(".open").click(function(){ $('.mainmenu').stop().animate({left: '0'}, 800); $( ".open" ).addclass( "close" ); $( ".open" ).removeclass( "open" ); }); $(".close").click(function(){ $('.mainmenu').stop().animate({left: '-180'}, 800); $( ".close" ).addclass( "open" ); $( ".close" ).removeclass( "close" ); });

assuming button has "open" class @ beginning. javascript code add together callback "open" class, , nil "close" class because there's no component such class yet. work around following:

function open() { $('.mainmenu').stop().animate({left: '0'}, 800); $( ".open" ).addclass( "close" ); $( ".open" ).removeclass( "open" ); $(".close").click(function(){ close(); }); } function close() { $('.mainmenu').stop().animate({left: '-180'}, 800); $( ".close" ).addclass( "open" ); $( ".close" ).removeclass( "close" ); $(".open").click(function(){ open(); }); } $(".open").click(function(){ open(); }); $(".close").click(function(){ close(); });

jquery onclick slide addclass removeclass

No comments:

Post a Comment