Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using JQuery .animate() to start at one value and end at another

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?

like image 818
Yarin Avatar asked Nov 18 '10 03:11

Yarin


People also ask

How can use a animate () method in jQuery?

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.

Can the jQuery animate () method be used to animate any CSS property?

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).

What do you enter in the second argument of the jQuery animate () function?

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.

What is STOP () in 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);


2 Answers

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.

like image 76
Alex Under Avatar answered Oct 17 '22 19:10

Alex Under


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);
like image 27
John Strickler Avatar answered Oct 17 '22 20:10

John Strickler