Is it possible to use jquery's .animate() method to animate properties from an (explicitly set) initial value to a final value, or must it always be from the current value to another value?
The jQuery animate() method is used to create custom animations. Syntax: $(selector).animate({params},speed,callback); The required params parameter defines the CSS properties to be animated.
The animate() is an inbuilt method in jQuery which is used to change the state of the element with CSS style. This method can also be used to change the CSS property to create the animated effect for the selected element. Syntax: (selector).
Step Function It accepts two arguments ( now and fx ), and this is set to the DOM element being animated. fx : a reference to the jQuery.
The jQuery stop() method is used to stop an animation or effect before it is finished. The stop() method works for all jQuery effect functions, including sliding, fading and custom animations. Syntax: $(selector). stop(stopAll,goToEnd);
You can simply specify start value by setting it as a pseudo-selector.
Let's consider that you need to animate some value that is not a CSS property from -100 to 100. In this case your code will be:
$({xyz: -100}).animate({xyz:100}, {duration:5000, complete:function(){
console.log("done");
}, step: function(now) {
//here you want to place your processing code for every frame of animation.
console.log("Anim now: "+now);
}});
You can see working example at JSFiddle here.
As far as I know it has to be current value to final value. But lets say you want to animate height from 20 to 100px; Just do:
$('#elem').css('height', '20px').animate({ height: 100 }, 1000);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With