Sub topic 17
How this differs from cross-document transitions
This chapter assumes you know document.startViewTransition(), the ::view-transition pseudo-element tree, and view-transition-name. It compares same-document transitions with cross-document ones, so you can pick the right tool and avoid mixing up their APIs.
Two kinds of view transition
A same-document transition animates between two DOM states inside one page. You trigger it from JavaScript, and your update callback changes the DOM. This is what the rest of the tutorial covered.
A cross-document transition animates between two separate pages during a normal navigation, such as clicking a link in a multi-page site. No JavaScript call starts it. Both pages opt in with CSS.
@view-transition {
navigation: auto;
}Both pages must include this rule, and they must be same-origin. The browser then snapshots the old page, loads the new one, and animates between them.
What stays the same
The rendering model is shared. Both kinds capture an old and a new snapshot, build the same pseudo-element tree, and run a default cross-fade on root. You style them with the same selectors, such as ::view-transition-old(root). You name shared elements with view-transition-name, and the names must still be unique within each page.
What differs
| Aspect | Same-document | Cross-document |
|---|---|---|
| Trigger | `document.startViewTransition()` | Navigation, with `@view-transition { navigation: auto; }` on both pages |
| Where the update happens | In your callback | In the browser's page load |
| Scope | One page, any origin | Same-origin pages only |
| `ViewTransition` object | Returned to you, with `ready`, `finished`, `updateCallbackDone` | Not returned by a call. Page events expose it instead |
| JavaScript control | Full | Limited to `pageswap` and `pagereveal` events |
| Fallback | Check for `document.startViewTransition` | Unsupported browsers ignore the CSS rule and navigate normally |
Controlling cross-document transitions
Since there is no call to hold a promise from, you reach the transition through two page events. pageswap fires on the old page just before it goes away. pagereveal fires on the new page before its first frame.
window.addEventListener('pagereveal', async (event) => {
if (!event.viewTransition) return;
await event.viewTransition.ready;
document.documentElement.animate(
{ opacity: [0, 1] },
{
duration: 300,
pseudoElement: '::view-transition-new(root)'
}
);
});Here event.viewTransition is the same kind of object you know from the same-document case.
Choosing between them
- Use same-document transitions for single-page apps, or when a state change happens without navigation, such as opening a panel or sorting a list.
- Use cross-document transitions for multi-page sites where you want animated page changes with almost no script.
- Do not expect state to carry across a cross-document transition. The new page loads fresh, so elements get their names from CSS or from the new page's own markup.
Exercise
A multi-page site has about.html and team.html on the same origin. You want a cross-fade when users click between them, with no JavaScript. What do you add, and where?
Show answer
Add @view-transition { navigation: auto; } to the CSS of both pages. Both pages must opt in. Browsers without support ignore the rule and navigate normally.