The Web Animations API is a browser interface for creating and controlling animations from JavaScript, built on the same timing and rendering model that powers CSS animations and CSS transitions. Among Web APIs, it is the standard way to start, pause, reverse, seek and inspect animations programmatically without resorting to manual frame loops.
The API is defined by the W3C Web Animations specification, and its core features are supported in all major browser engines. Its central method, Element.animate(), accepts keyframes and timing options and returns an Animation object that scripts can control precisely during playback.
How the Web Animations API models motion
The Web Animations API describes every animation as a combination of an effect and a timeline. The effect, usually a KeyframeEffect, states which properties change and how, while the timeline supplies the current time that drives progress.
The Animation object connects the two and acts as a playback controller. Scripts change its state through methods such as play(), pause(), reverse() and cancel(), and they read or set its currentTime and playbackRate properties for frame-accurate control.
The following table summarizes the main interfaces and their responsibilities.
| Interface | Role | Key members |
|---|---|---|
| Animation | Playback controller | play, pause, reverse, finished |
| KeyframeEffect | Defines property changes over time | target, getKeyframes, setKeyframes |
| AnimationEffect | Shared timing behavior | getTiming, updateTiming |
| DocumentTimeline | Default time source tied to the document | currentTime |
| ScrollTimeline | Progress driven by scroll position | source, axis |
Because CSS animations and transitions are exposed through the same model, document.getAnimations() returns declarative and scripted animations together. Scripts can therefore pause a CSS animation or read its progress without rewriting it in JavaScript.
Keyframes and timing options
Keyframes in the Web Animations API are written as an array of objects or an object of arrays, each listing CSS property values at points in time. An optional offset places a keyframe between 0 and 1, and a per-keyframe easing shapes the segment that follows it.
The second argument of animate() carries timing options that mirror CSS animation properties. Durations are given in milliseconds as numbers, unlike CSS, which uses strings with units.
| Option | Accepted values | Effect |
|---|---|---|
| duration | Milliseconds, for example 300 | Length of one iteration |
| easing | linear, ease-in-out, cubic-bezier() | Rate of change within an iteration |
| iterations | Number or Infinity | How many times the effect repeats |
| direction | normal, reverse, alternate | Order in which keyframes play |
| fill | none, forwards, backwards, both | Whether values apply outside active time |
| delay | Milliseconds, may be negative | Wait before the active phase begins |
The composite option, with values replace, add and accumulate, decides how an effect combines with other effects on the same property. Using add allows independent animations to stack, such as a hover scale on top of an entrance translation.
Comparison with CSS animations and script loops
The Web Animations API sits between declarative CSS animations and imperative requestAnimationFrame loops. It keeps the browser in charge of rendering while giving scripts the control that stylesheets lack.
The comparison below shows where each technique fits.
| Technique | Control at runtime | Compositor offloading | Best for |
|---|---|---|---|
| CSS transitions | Low, state changes only | Yes, for transform and opacity | Simple hover and focus effects |
| CSS animations | Limited, through class changes | Yes, for transform and opacity | Looping or decorative motion |
| Web Animations API | Full playback control | Yes, for transform and opacity | Interactive, sequenced motion |
| requestAnimationFrame | Complete, frame by frame | No, runs on the main thread | Physics, canvas and custom logic |
Animations of transform and opacity can usually run on the compositor thread, so they stay smooth even when the main thread is busy. Animating layout properties such as width or top forces layout recalculation on every frame regardless of the technique chosen.
Controlling playback and sequencing
Each Animation exposes a finished promise that resolves when playback ends, which makes sequencing straightforward with await. A script can run one animation, wait for it, and then start the next without timers or event bookkeeping.
Setting playbackRate to a negative value plays an animation backward, and assigning currentTime lets a script scrub an animation to any moment, for example while a user drags a slider. The updatePlaybackRate() method changes speed smoothly without visible jumps.
When an animation should leave its final values in place, commitStyles() writes the computed result into the inline style. This is preferable to fill: forwards, which keeps a filling animation alive and can block later style changes from taking effect.
Scroll-driven timelines and related APIs
Newer levels of the specification add ScrollTimeline and ViewTimeline, which replace time with scroll progress. An effect attached to such a timeline advances as the user scrolls, producing scroll-linked motion without scroll event handlers.
The Web Animations API also underpins the View Transitions API, whose pseudo-elements appear as ordinary Animation objects. Developers can therefore adjust or inspect view transitions with the same methods used for any other animation.
Common mistakes and limitations
Most issues with the Web Animations API stem from treating it as a style setter rather than a temporary effect layered over the computed style.
| Symptom | Cause | Fix |
|---|---|---|
| Element snaps back after finishing | Effect no longer applies after its end | Use commitStyles() and cancel() |
| Memory growth in long sessions | Many filling animations kept alive | Rely on automatic removal or cancel() |
| Jerky motion under load | Layout properties animated on main thread | Animate transform and opacity instead |
| Motion ignored accessibility needs | No check of user preference | Respect prefers-reduced-motion |
Browsers automatically remove replaced filling animations to limit memory use; the persist() method opts an animation out of this behavior. Some advanced features, including group and sequence effects proposed for later levels, remain unimplemented in browsers.
In simple terms
Websites often move things around: a menu slides in, a card fades, a button grows slightly when pressed. The Web Animations API is a remote control for that motion. A developer describes how something should move, and the browser handles the drawing.
Unlike a fixed recording, this remote control has pause, rewind, fast forward and a progress bar. The page can react to the user while the motion is happening, and the browser keeps it smooth because it does the heavy work itself.
A practical example
A front-end developer at an online furniture store builds a product configurator in which a sofa image slides between fabric options. The team first used CSS classes, but interrupting a slide halfway caused visible jumps. The developer replaces the classes with element.animate() calls that animate transform with a 350 millisecond duration.
When a shopper clicks quickly through fabrics, the script reads the running animation, calls reverse() or starts a new effect from the current position, and awaits the finished promise before updating the price panel. A check of prefers-reduced-motion shortens the duration to zero for users who request less motion.
In testing on mid-range phones, the slides stay smooth even while product data loads in the background. Interrupted transitions no longer jump, and the configurator code becomes shorter than the class-based version.