Sub topic 5
Customizing the animation with CSS
This chapter assumes you can call document.startViewTransition(), know the ::view-transition pseudo-element tree, and can write @keyframes animations. Here you replace the default cross-fade with your own timing and easing.
Why the default needs customizing
During a transition the browser puts the old and new states into pseudo-elements. For the unnamed page content, these are ::view-transition-old(root) and ::view-transition-new(root). The default animation is a 250ms cross-fade. To change it, write CSS rules that target these pseudo-elements. Your animation declarations override the browser's defaults.
Replacing the root cross-fade
The rules below set explicit durations and easing for the old and new images.
@keyframes fade-out { to { opacity: 0; } }
@keyframes fade-in { from { opacity: 0; } }
::view-transition-old(root) {
animation: 300ms ease-out both fade-out;
}
::view-transition-new(root) {
animation: 300ms ease-in both fade-in;
}Two details matter here:
- The old image fades out by animating to
opacity: 0. The new image fades in by animating fromopacity: 0, so its end state is the natural one. bothapplies the first keyframe before the animation starts and keeps the last keyframe after it ends. Without it, a frame can flash at the wrong opacity.
Which pseudo-element to target
Each part of the tree controls something different.
| Selector | What it controls |
|---|---|
| `::view-transition-group(name)` | Position and size animation of a named element |
| `::view-transition-old(name)` | The snapshot of the old state |
| `::view-transition-new(name)` | The live image of the new state |
Changing the group changes how a named element moves. Changing the old and new images changes how their content blends.
::view-transition-group(hero) {
animation-duration: 400ms;
animation-timing-function: ease-in-out;
}This rule only changes timing. The browser still generates the movement keyframes for the group, so you don't need to write them.
Common errors
- Selecting the wrong name.
::view-transition-old(root)does nothing for an element namedhero. Use the same name thatview-transition-nameassigns. - Putting the rules in the wrong place. The pseudo-elements belong to the document, so write the rules at the top level of your stylesheet. Don't scope them under a component selector.
- Forgetting that the new image is live. The new image keeps updating during the animation. The old image is a static snapshot.
- Expecting a transition when the callback fails. If the update callback throws or rejects, the transition is skipped and your animation never runs.
Exercise
A page uses ::view-transition-old(root) { animation: 300ms fade-out; } and the old content flashes back to full opacity right after fading out. What is missing, and why does it fix the problem?
Show answer
Add the both fill mode (animation: 300ms both fade-out;). Without a forwards fill, the animation stops applying at the end and the element returns to its normal opacity of 1 until the transition is removed. With both, the final keyframe (opacity: 0) stays in effect.