Full answer
element.animate(keyframes, options) is a JavaScript method that runs an animation with the same engine as CSS animations and transitions. It returns an Animation object.
Keyframes are an array of objects, or an object of arrays: [{ opacity: 0 }, { opacity: 1 }]. Options are the usual timing values: duration in milliseconds, easing, delay, iterations, direction and fill.
The Animation object gives control that CSS does not:
finishedis a promise that resolves when the animation ends, so you can chain steps withawait.play(),pause(),reverse(),cancel()andfinish()control playback, andplaybackRatechanges the speed.currentTimereads or sets the position, which allows scrubbing.commitStyles()writes the final values into the element's style, so the result stays after the animation is removed. By default, an animation withoutfill: "forwards"removes its effect at the end.element.getAnimations()lists the running animations, including CSS ones.
Prefer it when:
- The values are known only in script, such as a measured distance (as in FLIP).
- You need to wait for the end, to remove an element or start the next step.
- You need to pause, reverse or scrub.
Prefer CSS when the animation is a simple state change, because CSS is easier to read and to override with media queries. Both can run off the main thread when only transform and opacity change.
Respect the user's reduced motion setting: check matchMedia("(prefers-reduced-motion: reduce)").matches and skip or shorten the animation.
The animation fades the box from full opacity to 0.2. When the finished promise resolves, commitStyles writes the final opacity into the style, and cancel removes the animation. The box stays faded, and no animation is left on it.
Wait for finished before removing an element, so the animation is seen.
<div id="box" style="width:40px;height:40px;background:tomato"></div>
<script>
window.reduced = matchMedia("(prefers-reduced-motion: reduce)").matches;
window.anim = box.animate([{ opacity: 1 }, { opacity: 0.2 }], { duration: reduced ? 1 : 150, fill: "forwards" });
window.done = anim.finished.then(() => { anim.commitStyles(); anim.cancel(); return getComputedStyle(box).opacity; });
</script>Checked in Chrome 156 only, by reading the finished promise and the final opacity. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- animate() takes keyframes and timing options.
- The finished promise resolves at the end.
- play, pause, reverse, cancel and playbackRate control playback.
- commitStyles keeps the final values.
- CSS is simpler for plain state changes.
Common mistakes
- Expecting the end state to stay after the animation without fill or commitStyles.
- Using setTimeout to guess when an animation ends instead of the finished promise.
- Ignoring the reduced motion preference.
Follow-ups
- What does commitStyles do?
- How do you list the animations running on an element?
Tips
- Name the Animation object and its finished promise.
- Explain when you prefer WAAPI and when CSS is simpler.