One option is to use the step function specified in the jQuery animate ( API ) function to check the state during the animation.
JSFiddle example: http://jsfiddle.net/GweLA/13/
Js
var myTargetWidth = 500; $(document).ready(function(){ $('.sample').animate( { "width" : myTargetWidth },{ duration : 5000, step: function(now, fx) { if($(this).width() > 200){ myTargetWidth = 300; $(this).stop().animate({ "width" : myTargetWidth },1000); } } }); });
CSS
.sample{ width:20px; height:100px; background-color:#cccccc; }
HTML
<div class="sample"> width is supposed to be animated till 500 but it stops at 300 </div>
Solution 2:
After some research, I found that we can change the start and end properties of the fx parameter passed to the step functions to control the animation. This kind of smooths the animation, but not a very neat way to do it, though.
JSFiddle example: http://jsfiddle.net/GweLA/57/
Js
var myTargetWidth = 500; var isExecuted = false; $(document).ready(function(){ $('.sample').animate( { "width" : myTargetWidth },{ duration : 5000, queue : false, step: function(now, fx) {
Blackcursor
source share