html - .toggle jQuery issue, while switching the library Version of jQuery -
this code working fine when use : <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="text/javascript"></script>
$(document).ready(function(){ $('.mailing-form').toggle( function(){ $('#mailing-list-box').animate({ right: "0", }, 500); }, function(){ $('#mailing-list-box').animate({ right: "-256" }, 500); }); });
but when chose switch version of jquery : <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
toggle
stops working.
then tried wrap toggle
inside click
event, mailing-list-box
div keeps coming , forth multiple times.
('.menu-btn').click(function(e) { $('.menu-btn').toggle( function(){ $('.timeline-box2').animate({ left: "0", }, 500); }, function(){ $('.timeline-box2').animate({ left: "-300" }, 500); }); });
you can achieve same behaviour without old .toggle()
way:
$(document).ready(function(){ var box = $('.timeline-box2'); $('.menu-btn').on('click', function() { // set animation value depending on presence of class 'forward' var pos = box.hasclass('forward') && -300 || 0; box.toggleclass('forward'): // switch class 'forward' on/off @ each click box.animate({left: pos}, 500); }); });
this way each click adds new animation animation queue. means if click 3 times box goes 1 time left=0
, left=-300
, again left=0
. if dont want add animations on click interrupt running 1 , go reverse use jquery.fn.stop before animate:
box.stop().animate({left: pos}, 500);
Comments
Post a Comment