Sub topic 11
Directional animations with transition types
This chapter assumes you have enabled cross-document transitions with @view-transition { navigation: auto; } on both pages. It also assumes you know how ::view-transition-old(root) and ::view-transition-new(root) work, and that you have used the pageswap and pagereveal events.
Why one animation is not enough
A single slide-left animation looks right when the user opens a post. It looks wrong when they press Back, because the page should then move the other way. CSS alone cannot tell the two navigations apart. Transition types fix this. A type is a string label attached to a running transition. CSS can match on it.
Setting types in JavaScript
You label the transition in the events you already know. A type set during pageswap exists only on the outgoing page's transition. The animations run in the incoming document, so that document needs the type too. Set it in pagereveal as well.
function directionFor(path) {
return path.startsWith('/posts/') ? 'forward' : 'back';
}
window.addEventListener('pageswap', (event) => {
if (!event.viewTransition || !event.activation) return;
const to = new URL(event.activation.entry.url);
event.viewTransition.types.add(directionFor(to.pathname));
});
window.addEventListener('pagereveal', (event) => {
if (!event.viewTransition) return;
const entry = window.navigation?.activation?.entry;
if (!entry) return;
const to = new URL(entry.url);
event.viewTransition.types.add(directionFor(to.pathname));
});Include this script on every page that takes part. Both guards matter. event.viewTransition is null when no transition will run, such as a cross-origin navigation.
Matching types in CSS
The :active-view-transition-type() pseudo-class is true on the root element only while a transition with that type is running. Put it before the pseudo-element.
@view-transition {
navigation: auto;
}
@keyframes slide-out-left {
to { opacity: 0; transform: translateX(-40px); }
}
@keyframes slide-in-right {
from { opacity: 0; transform: translateX(40px); }
}
@keyframes slide-out-right {
to { opacity: 0; transform: translateX(40px); }
}
@keyframes slide-in-left {
from { opacity: 0; transform: translateX(-40px); }
}
html:active-view-transition-type(forward)::view-transition-old(root) {
animation: 250ms ease-in both slide-out-left;
}
html:active-view-transition-type(forward)::view-transition-new(root) {
animation: 250ms ease-out both slide-in-right;
}
html:active-view-transition-type(back)::view-transition-old(root) {
animation: 250ms ease-in both slide-out-right;
}
html:active-view-transition-type(back)::view-transition-new(root) {
animation: 250ms ease-out both slide-in-left;
}A navigation with no matching type gets the default crossfade. The page degrades safely if the script fails to run.
Common errors
- Type set on one page only. The animation rules apply in the new document, so a
pageswap-only setup often shows the default crossfade. Add the type inpagerevealtoo. - Different rules on each page. Both pages must load the same transition CSS, or the result depends on which page is the destination.
- Direction logic that is too simple. A path prefix check works for list-to-detail. Sites with many page levels need a rule that compares the source and destination, such as depth or an ordered list of URLs.
- Types are not names. A type selects animation rules for the whole transition. It does not give an element a
view-transition-name.
Exercise
A user opens a post and the slide-in works. When they press Back, the page crossfades instead of sliding. The script only adds a type in a pageswap listener. What is the likely cause, and what is the fix?
Show answer
The animation rules run in the incoming document, and a type added in pageswap exists only on the outgoing page's transition. On Back, the list page has no type, so no rule matches and the default crossfade runs. Add the same type in a pagereveal listener, read from navigation.activation.entry.url, and load the script on both pages.