Element.animate()

The Element interface's animate() method is a shortcut method which creates a new Animation, applies it to the element, then plays the animation. It returns the created Animation object instance.

Note: Elements can have multiple animations applied to them. You can get a list of the animations that affect an element by calling Element.getAnimations().

Syntax

animate(keyframes, options)

Parameters

keyframes

Either an array of keyframe objects, or a keyframe object whose properties are arrays of values to iterate over. See Keyframe Formats for more details.

options

Either an integer representing the animation's duration (in milliseconds), or an Object containing one or more timing properties described in the KeyframeEffect() options parameter and/or the following options:

id Optional

A property unique to animate(): a DOMString with which to reference the animation.

Return value

Returns an Animation.

Examples

In the demo Down the Rabbit Hole (with the Web Animation API), we use the convenient animate() method to immediately create and play an animation on the #tunnel element to make it flow upwards, infinitely. Notice the array of objects passed as keyframes and also the timing options block.

document.getElementById("tunnel").animate([
  // keyframes
  { transform: 'translateY(0px)' },
  { transform: 'translateY(-300px)' }
], {
  // timing options
  duration: 1000,
  iterations: Infinity
});

Implicit to/from keyframes

In newer browser versions, you are able to set a beginning or end state for an animation only (i.e. a single keyframe), and the browser will infer the other end of the animation if it is able to. For example, consider this simple animation — the Keyframe object looks like so:

let rotate360 = [
  { transform: 'rotate(360deg)' }
];

We have only specified the end state of the animation, and the beginning state is implied.

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
animate
36
79
48
No
23
13.1
11.1
37
36
48
24
13.4
11.3
3.0
implicit_tofrom
No
Currently Chrome Canary only
No
75
No
No
13.1
Implementation seems somewhat buggy. More information will follow when available.
No
No
Currently Chrome Canary only
No
No
13.4
Implementation seems somewhat buggy. More information will follow when available.
No
options_composite_parameter
79
79
80
63-79
No
66
No
No
79
63-79
No
No
No
options_id_parameter
50
79
48
No
37
No
50
50
48
37
No
5.0
options_iterationComposite_parameter
No
No
80
63-79
No
No
No
No
No
63-79
No
No
No
options_pseudoElement_parameter
81
A valid animation object is returned but the targeted pseudoelement is not visually animated.
81
A valid animation object is returned but the targeted pseudoelement is not visually animated.
75
No
68
A valid animation object is returned but the targeted pseudoelement is not visually animated.
No
No
81
A valid animation object is returned but the targeted pseudoelement is not visually animated.
No
No
No
No

See also

© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Element/animate