getAnimations Finds Running CSS Transitions
List active CSS transitions with getAnimations and await their finished promises instead of hardcoding durations; snapshot after the new style has rendered.
Code
// CSS on the element: transition: opacity 240ms ease;
const panel = document.createElement('div');
panel.style.width = '40px';
panel.style.height = '40px';
panel.style.transition = 'opacity 240ms ease';
panel.style.opacity = '0';
document.body.append(panel);
function nextFrame() {
return new Promise((resolve) => requestAnimationFrame(resolve));
}
function waitForTransitions(el) {
// getAnimations({ subtree: true }) also collects descendant animations
const running = el.getAnimations({ subtree: true });
console.log('running:', running.map((a) => a.constructor.name || a.playState));
return Promise.all(running.map((a) => a.finished));
}
// A CSS transition only exists once the browser has (1) rendered the old
// value and (2) put the new value through style recalc. Calling
// getAnimations earlier — same frame, or before the change is rendered —
// returns an empty list and Promise.all resolves immediately.
// Frame 1 paints the panel at opacity 0; frame 2 carries the change (the
// transition starts in that frame's style recalc); frame 3 is the first
// moment the snapshot reliably sees the running CSSTransition.
nextFrame()
.then(() => nextFrame())
.then(() => {
panel.style.opacity = '1';
return nextFrame();
})
.then(() => waitForTransitions(panel))
.then(() => {
console.log('opacity settled:', getComputedStyle(panel).opacity);
});
setTimeout(() => {
// By now the 240ms transition is done; a new snapshot finds none.
console.log('later snapshot:', panel.getAnimations().length === 0);
}, 400);How the snippet works
Everything happens inside the functions nextFrame and waitForTransitions.
Asynchronous boundaries use await, so failures surface as rejected promises at the call site instead of being swallowed.
Returns a newly built value, leaving the inputs as they were.
Points of care
- The snippet leans on
document,requestAnimationFrame— browser-only surfaces a bare Node process cannot provide.
Using it
Signature: function nextFrame()
The demo output is labeled (running:, opacity settled:, later snapshot:); each label names the call that printed it.
Key statements
return new Promise((resolve) => requestAnimationFrame(resolve));
return Promise.all(running.map((a) => a.finished));
.then(() => {
panel.style.opacity = '1';
return nextFrame();
})
Interactive Tools for This Topic
Aspect Ratio CalculatorSimplify a width-to-height ratio, show its decimal value, and derive a matching height for a new width.Border Radius PreviewPreview four-corner border-radius values on a live box and copy the generated shorthand border-radius CSS declaration.Box Shadow GeneratorBuild CSS box-shadow values with offset, blur, spread, color, opacity, and inset controls, with a live preview of the result.CSS Color Name LookupTranslate between CSS keyword color names and hex values with RGB and HSL conversions, exact name matching, and nearest-color suggestions.
FAQ
- Does `nextFrame` modify its input?
- No. The code builds and returns a new value and leaves the arguments untouched, so it is safe to call on shared or frozen data structures.
- Does getAnimations Finds Running CSS Transitions run in plain Node.js?
- No — it touches `document` and `requestAnimationFrame`, which exist only in the browser. Run it in a page (or the DevTools console) instead of a bare Node process.
Report a problem with this exampleOpens a pre-filled issue on GitHub.