Sub topic 15
Choosing animations with transition types
This chapter assumes you can call document.startViewTransition(), you know the ::view-transition-old() and ::view-transition-new() pseudo-elements, and you can write CSS @keyframes animations. It covers how to tag a transition with a type and how to pick animations based on that type.
The problem types solve
One page often needs several kinds of transitions. Moving to the next item should slide left. Moving to the previous item should slide right. Opening a settings panel might just fade. All three run through the same root pseudo-elements, so a single rule for ::view-transition-old(root) cannot serve all of them.
A transition type is a short string you attach to one transition. CSS can then match that string and apply different animations.
Tagging a transition
Pass an options object instead of a bare callback. The update function is the same DOM update you used before. The types array holds your labels.
document.startViewTransition({
update: () => updateView(),
types: ['slide-forward']
});You can set several types on one transition, for example ['slide-forward', 'fast'].
Matching a type in CSS
While a transition with a given type is running, the :active-view-transition-type() pseudo-class matches on the root element. Use it to scope your animation rules.
@keyframes slide-out-left { to { transform: translateX(-40px); opacity: 0; } }
@keyframes slide-in-right { from { transform: translateX(40px); opacity: 0; } }
@keyframes slide-out-right { to { transform: translateX(40px); opacity: 0; } }
@keyframes slide-in-left { from { transform: translateX(-40px); opacity: 0; } }
:root:active-view-transition-type(slide-forward) {
&::view-transition-old(root) {
animation: 250ms ease-in both slide-out-left;
}
&::view-transition-new(root) {
animation: 250ms ease-out both slide-in-right;
}
}
:root:active-view-transition-type(slide-back) {
&::view-transition-old(root) {
animation: 250ms ease-in both slide-out-right;
}
&::view-transition-new(root) {
animation: 250ms ease-out both slide-in-left;
}
}The rules apply only while a transition carries that type. When the transition ends, the type is gone and the default cross-fade returns.
Common errors
- Passing a function and expecting types. A plain callback has no types. Use the options object.
- Unsupported browsers. Types need newer browser support than the basic API. Where
startViewTransition()exists but types do not, thetypeskey is ignored and the default cross-fade runs. Keep that default acceptable. - Forgetting direction logic. The browser does not know "forward" or "back". Your code decides, for instance by comparing the old and new index.
Exercise
A carousel calls document.startViewTransition(() => show(next)) for both Next and Previous buttons. What two changes let Previous slide the other way?
Show answer
Change both calls to the options form with a type, for example { update: () => show(next), types: ['slide-forward'] } and { update: () => show(prev), types: ['slide-back'] }. Then add CSS rules scoped with :root:active-view-transition-type(slide-forward) and :root:active-view-transition-type(slide-back) that apply opposite slide animations.