Web Animations API

On this page 8

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.

InterfaceRoleKey members
AnimationPlayback controllerplay, pause, reverse, finished
KeyframeEffectDefines property changes over timetarget, getKeyframes, setKeyframes
AnimationEffectShared timing behaviorgetTiming, updateTiming
DocumentTimelineDefault time source tied to the documentcurrentTime
ScrollTimelineProgress driven by scroll positionsource, axis
Core interfaces of the Web Animations API

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.

OptionAccepted valuesEffect
durationMilliseconds, for example 300Length of one iteration
easinglinear, ease-in-out, cubic-bezier()Rate of change within an iteration
iterationsNumber or InfinityHow many times the effect repeats
directionnormal, reverse, alternateOrder in which keyframes play
fillnone, forwards, backwards, bothWhether values apply outside active time
delayMilliseconds, may be negativeWait before the active phase begins
Frequently used timing options

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.

TechniqueControl at runtimeCompositor offloadingBest for
CSS transitionsLow, state changes onlyYes, for transform and opacitySimple hover and focus effects
CSS animationsLimited, through class changesYes, for transform and opacityLooping or decorative motion
Web Animations APIFull playback controlYes, for transform and opacityInteractive, sequenced motion
requestAnimationFrameComplete, frame by frameNo, runs on the main threadPhysics, canvas and custom logic
Animation techniques in the browser

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.

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.

SymptomCauseFix
Element snaps back after finishingEffect no longer applies after its endUse commitStyles() and cancel()
Memory growth in long sessionsMany filling animations kept aliveRely on automatic removal or cancel()
Jerky motion under loadLayout properties animated on main threadAnimate transform and opacity instead
Motion ignored accessibility needsNo check of user preferenceRespect prefers-reduced-motion
Typical problems and their fixes

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.