Sub topic 12
Animating transition pseudo-elements with JavaScript
This chapter assumes you know promises and async/await, CSS @keyframes, and the pseudo-element tree from earlier chapters. You should also know that document.startViewTransition() returns a ViewTransition object with a ready promise.
Why use JavaScript
CSS animations cover most cases. JavaScript helps when the animation depends on runtime values. Examples are the click position, the scroll offset, or a duration read from user settings. The Web Animations API (WAAPI) lets you animate the transition pseudo-elements with those values.
The key rule: wait for `ready`
The pseudo-elements do not exist until the browser has captured both states and built the tree. The ready promise resolves at that moment, just before the animation starts. If you call animate() earlier, it has no pseudo-element to target.
const transition = document.startViewTransition(() => updateView());
await transition.ready;
document.documentElement.animate(
{ clipPath: ['inset(0 0 100% 0)', 'inset(0)'] },
{
duration: 400,
easing: 'ease-in',
pseudoElement: '::view-transition-new(root)'
}
);The call runs on document.documentElement, because the transition tree hangs off the root element. The pseudoElement option selects which part of the tree to animate. Here the new snapshot is revealed from top to bottom with a clip-path.
Turn off the default animation
The browser still applies its default cross-fade. A fade running together with your clip-path reveal looks muddy. Disable the default animation and the blend mode in CSS:
::view-transition-old(root),
::view-transition-new(root) {
animation: none;
mix-blend-mode: normal;
}Now your JavaScript animation is the only motion on screen.
Using runtime values
A common use is a circular reveal from the click point. Compute the radius from the click position, then animate clip-path with it.
async function revealFrom(event, update) {
if (!document.startViewTransition) {
update();
return;
}
const x = event.clientX;
const y = event.clientY;
const radius = Math.hypot(
Math.max(x, innerWidth - x),
Math.max(y, innerHeight - y)
);
const transition = document.startViewTransition(update);
await transition.ready;
document.documentElement.animate(
{ clipPath: [`circle(0 at ${x}px ${y}px)`, `circle(${radius}px at ${x}px ${y}px)`] },
{
duration: 500,
easing: 'ease-in',
pseudoElement: '::view-transition-new(root)'
}
);
}The radius reaches the farthest corner, so the circle always covers the full viewport.
Common errors
- Calling
animate()beforeawait transition.ready. The pseudo-element is missing, so nothing visible happens. - Forgetting
pseudoElement. The animation then applies to the root element itself. - Leaving the default animation on. It competes with your own.
- Awaiting
readywhen the transition was skipped. In that casereadyrejects, so wrap the call intry/catchor use.catch().
Exercise
Why must you await transition.ready before calling document.documentElement.animate() with a pseudoElement option?
Show answer
The transition pseudo-elements are created only after the browser captures the new state. The ready promise resolves once they exist. Before that, the target pseudo-element is missing, so the animation has nothing to apply to.