Sub topic 5
Writing your own page animations
This chapter assumes you know CSS @keyframes, how multi-page sites load each URL as a separate document, and how to opt in with @view-transition { navigation: auto; } on both pages. If the opt-in is new to you, read that chapter first.
What you are animating
During a transition the browser captures two snapshots of the page, the old one and the new one. It places them in a tree of pseudo-elements. For the whole page, the pair is named root:
::view-transition-old(root)is the snapshot of the page you are leaving.::view-transition-new(root)is the snapshot of the page you are arriving at.
The default animation fades the old one out and the new one in. You replace it by assigning your own animation to these pseudo-elements. They are styled with ordinary CSS, and you write the rules in the stylesheet of the page that is displayed when the transition runs.
A slide animation
This example slides the old page out to the left while fading it, then slides the new page in from the right.
@view-transition {
navigation: auto;
}
@keyframes slide-out {
to {
opacity: 0;
transform: translateX(-40px);
}
}
@keyframes slide-in {
from {
opacity: 0;
transform: translateX(40px);
}
}
::view-transition-old(root) {
animation: 200ms ease-in both slide-out;
}
::view-transition-new(root) {
animation: 300ms ease-out both slide-in;
}Include this in the stylesheet that both pages load. Each keyframe block gives only one end of the motion. slide-out has no from, so the animation starts at the element's current state. slide-in has no to, so it ends at the normal position.
Why `both` matters
The both fill mode keeps the first keyframe applied before the animation starts and the last one after it ends. Without it, the new page can flash at full opacity before slide-in begins. Keep both on these animations.
Keep durations short
Navigation waits for nothing during the animation, but the user does. Aim for roughly 150 to 400 ms. The old snapshot is a still image, so a long exit delays the moment the user sees live content.
Common errors
- Rules only on one page. The animation runs from the destination page's CSS for the arriving snapshot, and the departing page's CSS for the leaving one. Put the rules in a shared stylesheet.
- Animating the wrong target. Setting
animationon::view-transition-group(root)moves both snapshots together. Targetoldandnewseparately when they need different motion. - Animating layout properties. Stick to
opacityandtransform. Properties such aswidthcan behave unpredictably on snapshots, and they are costly. - Missing
both. This causes a visible flash, as described above. - Forgetting reduced motion. A later chapter covers it. Plan to disable these animations for users who ask for less movement.
Check your understanding
Exercise
You write ::view-transition-new(root) { animation: 300ms ease-out slide-in; }, where slide-in only has a from keyframe with opacity: 0. On navigation, the new page appears briefly at full opacity and then jumps to transparent before fading in. What is the cause, and how do you fix it?
Show answer
The animation has no fill mode, so before it starts (and during any start delay) the element shows its normal state, which is fully opaque. Add both (or at least backwards) to the shorthand: animation: 300ms ease-out both slide-in;. The first keyframe then applies before the animation begins.