This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2020.
* Some parts of this feature may have varying levels of support.
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().
animate(keyframes, options)
keyframesEither an array of keyframe objects, or a keyframe object whose properties are arrays of values to iterate over. See Keyframe Formats for more details.
optionsEither 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 OptionalA property unique to animate(): A string with which to reference the animation.
rangeEnd OptionalSpecifies the end of an animation's attachment range along its timeline, i.e., where along the timeline an animation will end. The JavaScript equivalent of the CSS animation-range-end property. rangeEnd can take several different value types, as follows:
A string that can be normal (meaning no change to the animation's attachment range), a CSS <length-percentage> representing an offset, a <timeline-range-name>, or a <timeline-range-name> with a <length-percentage> following it. For example: "normal", "entry", or "cover 100%".
See animation-range for a detailed description of the available values. Also check out the View Timeline Ranges Visualizer, which shows exactly what the different values mean in an easy visual format.
An object containing rangeName (a string) and offset (a CSSNumericValue) properties representing a <timeline-range-name> and <length-percentage>, as described in the previous bullet. For example: { rangeName: "entry", offset: CSS.percent("100") }.
A CSSNumericValue representing an offset, for example: CSS.percent("100").
rangeStart OptionalSpecifies the start of an animation's attachment range along its timeline, i.e., where along the timeline an animation will start. The JavaScript equivalent of the CSS animation-range-start property. rangeStart can take the same value types as rangeEnd.
timeline OptionalA property unique to animate(): The AnimationTimeline to associate with the animation. Defaults to Document.timeline. The JavaScript equivalent of the CSS animation-timeline property.
Returns an Animation.
In this example we use the animate() method to rotate and scale an element.
<div class="newspaper">Spinning newspaper<br />causes dizziness</div>
html,
body {
height: 100%;
}
body {
display: flex;
justify-content: center;
align-items: center;
background-color: black;
}
.newspaper {
padding: 0.5rem;
text-transform: uppercase;
text-align: center;
background-color: white;
cursor: pointer;
}
const newspaperSpinning = [
{ transform: "rotate(0) scale(1)" },
{ transform: "rotate(360deg) scale(0)" },
];
const newspaperTiming = {
duration: 2000,
iterations: 1,
};
const newspaper = document.querySelector(".newspaper");
newspaper.addEventListener("click", () => {
newspaper.animate(newspaperSpinning, newspaperTiming);
});
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,
},
);
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.
Typical usage of the timeline, rangeStart, and rangeEnd properties might look like this:
const img = document.querySelector("img");
const timeline = new ViewTimeline({
subject: img,
axis: "block",
});
img.animate(
{
opacity: [0, 1],
transform: ["scaleX(0)", "scaleX(1)"],
},
{
fill: "both",
duration: 1,
timeline,
rangeStart: "cover 0%",
rangeEnd: "cover 100%",
},
);
| Specification |
|---|
| Web Animations> # dom-animatable-animate> |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | |
animate |
36 | 79 | 48 | 23 | 13.1 | 36 | 48 | 24 | 13.4 | 3.0 | 37 | 13.4 |
implicit_tofrom |
84 | 84 | 75 | 70 | 13.1Implementation seems somewhat buggy. More information will follow when available. |
84 | 79 | 60 | 13.4Implementation seems somewhat buggy. More information will follow when available. |
14.0 | 84 | 13.4Implementation seems somewhat buggy. More information will follow when available. |
options_composite_parameter |
84 | 84 | 80 | 70 | 16 | 84 | 80 | 60 | 16 | 14.0 | 84 | 16 |
options_id_parameter |
50 | 79 | 48 | 37 | 13.1 | 50 | 48 | 37 | 13.4 | 5.0 | 50 | 13.4 |
options_iterationComposite_parameter |
No | No | 80 | No | No | No | 80 | No | No | No | No | No |
options_pseudoElement_parameter |
84 | 84 | 75 | 70 | 14 | 84 | 79 | 60 | 14 | 14.0 | 84 | 14 |
options_rangeEnd_parameter |
115 | 115 | No | 101 | 26 | 115 | No | 77 | 26 | 23.0 | 115 | 26 |
options_rangeStart_parameter |
115 | 115 | No | 101 | 26 | 115 | No | 77 | 26 | 23.0 | 115 | 26 |
options_timeline_parameter |
85 | 85 | No | 71 | 16 | 85 | No | 60 | 16 | 14.0 | 85 | 16 |
AnimationElement.getAnimations()animation-range-end, animation-range-start, animation-timeline
© 2005–2025 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