jQuery .animate()
Learn all about the jQuery function .animate().
The .animate()
method allows us to create animation effects on any numeric CSS property. The only required parameter is a plain object of CSS properties. This object is similar to the one that can be sent to the .css()
method, except that the range of properties is more restrictive.
Animation Properties and Values
All animated properties should be animated to a single numeric value, except as noted below; most properties that are non-numeric cannot be animated using basic jQuery functionality (For example, width
, height
, or left
can be animated but background-color
cannot be, unless the jQuery.Color plugin is used). Property values are treated as a number of pixels unless otherwise specified. The units em
and %
can be specified where applicable.
In addition to style properties, some non-style properties such as scrollTop
and scrollLeft
, as well as custom properties, can be animated.
Shorthand CSS properties (e.g. font, background, border) are not fully supported. For example, if you want to animate the rendered border width, at least a border style and border width other than "auto" must be set in advance. Or, if you want to animate font size, you would use fontSize
or the CSS equivalent 'font-size'
rather than simply 'font'
.
In addition to numeric values, each property can take the strings 'show'
, 'hide'
, and 'toggle'
. These shortcuts allow for custom hiding and showing animations that take into account the display type of the element. In order to use jQuery’s built-in toggle state tracking, the 'toggle'
keyword must be consistently given as the value of the property being animated.
Animated properties can also be relative. If a value is supplied with a leading +=
or -=
sequence of characters, then the target value is computed by adding or subtracting the given number from the current value of the property.
Note: Unlike shorthand animation methods such as .slideDown()
and .fadeIn()
, the .animate()
method does not make hidden elements visible as part of the effect. For example, given $( "someElement" ).hide().animate({height: "20px"}, 500)
, the animation will run, but the element will remain hidden.
Duration
Durations are given in milliseconds; higher values indicate slower animations, not faster ones. The default duration is 400
milliseconds. The strings 'fast'
and 'slow'
can be supplied to indicate durations of 200
and 600
milliseconds, respectively.
Callback Functions
If supplied, the start
, step
, progress
, complete
, done
, fail
, and always
callbacks are called on a per-element basis; this
is set to the DOM element being animated. If no elements are in the set, no callbacks are called. If multiple elements are animated, the callback is executed once per matched element, not once for the animation as a whole. Use the .promise()
method to obtain a promise to which you can attach callbacks that fire once for an animated set of any size, including zero elements.
Basic Usage
To animate any element, such as a simple image:
1
2
3
4
5
|
|
To animate the opacity, left offset, and height of the image simultaneously:
1
2
3
4
5
6
7
8
9
|
|
Note that the target value of the height
property is 'toggle'
. Since the image was visible before, the animation shrinks the height to 0 to hide it. A second click then reverses this transition:
The opacity
of the image is already at its target value, so this property is not animated by the second click. Since the target value for left
is a relative value, the image moves even farther to the right during this second animation.
Directional properties (top
, right
, bottom
, left
) have no discernible effect on elements if their position
style property is static
, which it is by default.
Note: The jQuery UI project extends the .animate()
method by allowing some non-numeric styles such as colors to be animated. The project also includes mechanisms for specifying animations through CSS classes rather than individual attributes.
Note: if attempting to animate an element with a height or width of 0px, where contents of the element are visible due to overflow, jQuery may clip this overflow during animation. By fixing the dimensions of the original element being hidden however, it is possible to ensure that the animation runs smoothly. A clearfix can be used to automatically fix the dimensions of your main element without the need to set this manually.
Step Function
The second version of .animate()
provides a step
option — a callback function that is fired at each step of the animation. This function is useful for enabling custom animation types or altering the animation as it is occurring. It accepts two arguments (now
and fx
), and this
is set to the DOM element being animated.
-
now
: the numeric value of the property being animated at each step -
fx
: a reference to thejQuery.fx
prototype object, which contains a number of properties such aselem
for the animated element,start
andend
for the first and last value of the animated property, respectively, andprop
for the property being animated.
Note that the step
function is called for each animated property on each animated element. For example, given two list items, the step
function fires four times at each step of the animation:
1
2
3
4
5
6
7
8
9
|
|
Easing
The remaining parameter of .animate()
is a string naming an easing function to use. An easing function specifies the speed at which the animation progresses at different points within the animation. The only easing implementations in the jQuery library are the default, called swing
, and one that progresses at a constant pace, called linear
. More easing functions are available with the use of plug-ins, most notably the jQuery UI suite.
Per-property Easing
As of jQuery version 1.4, you can set per-property easing functions within a single .animate()
call. In the first version of .animate()
, each property can take an array as its value: The first member of the array is the CSS property and the second member is an easing function. If a per-property easing function is not defined for a particular property, it uses the value of the .animate()
method’s optional easing argument. If the easing argument is not defined, the default swing
function is used.
For example, to simultaneously animate the width and height with the swing
easing function and the opacity with the linear
easing function:
1
2
3
4
5
6
7
8
9
|
|
In the second version of .animate()
, the options object can include the specialEasing
property, which is itself an object of CSS properties and their corresponding easing functions. For example, to simultaneously animate the width using the linear
easing function and the height using the easeOutBounce
easing function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
|
As previously noted, a plugin is required for the easeOutBounce
function.