Saturday, 15 February 2014

javascript - How to animate back my buttons -



javascript - How to animate back my buttons -

a little problem... think! have menu of buttons. when 1 clicked, moves (animates). when 1 clicked, need first 1 move again. far moves when clicked, not again. code:

$(document).ready(function(){ var vari = $(this).attr('name'); $('.nav_btn').click(function(){ $(this).animate({left:'50%'}); }); });

with jquery, seems easy me. is, give buttons same classname , loop through of them when 1 of them clicked. because can element collection much this:

$(document).ready(function(){ var vari = $(this).attr('name'); $('.nav_btn').click(function(){ var buttons = $('.nav_btn'); for(var i=0; i<buttons.length; i++) { if(buttons[i]!=this) //avoid animating clicked button twice! think of performance $(buttons[i].animate({left:'0%'}); } $(this).animate({left:'50%'}); }); });

javascript jquery button jquery-animate

No comments:

Post a Comment