Sub topic 17
How this differs from cross-document transitions
This chapter assumes you have read the earlier chapters on document.startViewTransition(), the ::view-transition pseudo-element tree, and view-transition-name. You should be comfortable with promises and with DevTools. Everything you learned about styling the pseudo-elements carries over. What changes is how a transition starts, who controls the DOM update, and what you can do from JavaScript.
Same idea, different trigger
A same-document transition happens inside one page. Your script calls document.startViewTransition(), the browser captures the old state, your callback changes the DOM, and the browser animates to the new state.
A cross-document transition happens during a normal navigation, for example when a user clicks a link from /products to /products/42. No script calls a start function. The "update" is the navigation itself: the old document is replaced by a new one. The browser captures the old page, loads the new page, captures it, and animates between the two.
Both kinds use the same snapshot model, the same pseudo-element tree, and the same view-transition-name rules. A ::view-transition-old(root) rule works the same way in both.
Opting in with CSS
Same-document transitions are opt-in through JavaScript, because only your code calls startViewTransition(). Cross-document transitions are opt-in through CSS. Both the page you leave and the page you arrive at must include this at-rule:
@view-transition {
navigation: auto;
}Both pages must also be on the same origin. Navigations to another origin never animate. If only one of the two pages has the rule, the navigation happens normally with no transition. The default is navigation: none.
Once the rule is present, every eligible same-origin navigation gets the default root cross-fade. Eligible navigations include link clicks, form submissions, and back and forward traversals. Navigations that involve a cross-origin redirect are not eligible.
No callback, so new hooks
A cross-document transition has no update callback, because you do not control the moment the DOM changes. The browser does it for you by loading the next page. Two events replace the callback as your hooks into the process.
pageswapfires on the old page just before it is hidden.event.viewTransitionis the transition object, ornullif there will be no transition.event.activationdescribes the navigation, including the destination URL.pagerevealfires on the new page after it is initialized and before it renders for the first time.event.viewTransitionis again the transition object ornull.
These events fire on every navigation, including ones without a transition, so always check event.viewTransition first.
Shared elements across pages
In the same-document case, you move a view-transition-name from one element to another inside the update callback. Across documents, the two elements live in different pages, so each page must assign the name itself. A common pattern is to name the clicked thumbnail in pageswap and name the detail image in pagereveal.
window.addEventListener('pageswap', (event) => {
if (!event.viewTransition) return;
const target = new URL(event.activation.entry.url);
const match = target.pathname.match(/^\/products\/(\d+)$/);
if (!match) return;
const thumb = document.querySelector(`[data-id="${match[1]}"] img`);
if (thumb) thumb.style.viewTransitionName = 'hero';
});
window.addEventListener('pagereveal', (event) => {
if (!event.viewTransition) return;
const detailImage = document.querySelector('.detail-image');
if (detailImage) {
detailImage.style.viewTransitionName = 'hero';
} else {
event.viewTransition.skipTransition();
}
});The first handler lives in the list page's script and the second in the detail page's script. If a page can be both source and destination, put both handlers in a shared script. The name is set on the old page before the browser takes the old snapshot. The name is set on the new page before its first render, so the new snapshot sees it.
Static names also work. If the header on every page has view-transition-name: site-header in the stylesheet, it stays put while the rest of the page cross-fades. No events are needed for that.
Comparing the two models
| Aspect | Same-document | Cross-document |
|---|---|---|
| Trigger | Your call to `startViewTransition()` | A navigation, once both pages opt in |
| Opt-in | JavaScript | `@view-transition { navigation: auto; }` in CSS on both pages |
| DOM update | Your callback | The browser loads the next page |
| JavaScript hooks | Return value of `startViewTransition()` | `pageswap` and `pagereveal` events |
| Origin | One page | Same origin only |
| Typical use | SPAs, in-page state changes | Multi-page sites, server-rendered apps |
| State between old and new | Shared JS memory | None, separate documents |
The last row matters in practice. In a same-document transition you can keep a variable that remembers the clicked card. Across documents, that memory is gone after the navigation. You must pass information through the URL, sessionStorage, or the event.activation data.
Timing and the cost of a slow page
In a same-document transition the DOM update is under your control. Across documents the new page's network and rendering time sit inside the transition. While the browser waits, the old snapshot stays on screen. If the new page takes too long, the browser gives up and skips the transition, so the user simply sees the new page. Browsers apply a timeout for this.
The new page can also flash an unstyled or incomplete state before the animation starts. To avoid that, block the first render until important content exists, using a render-blocking link:
<link rel="expect" href="#main-content" blocking="render">The browser waits until an element with id="main-content" is parsed before it first renders the page. Use this sparingly. It delays every first paint on that page, including those with no transition.
What stays the same
- The pseudo-element tree and all the
::view-transition-*selectors. - Names must be unique among rendered elements in each document's state.
prefers-reduced-motionhandling. Your CSS rule that disables the animations applies in both models.- Feature detection. For cross-document, you do not test for
startViewTransition. Test for the events instead, or let the CSS at-rule be ignored. Browsers that do not understand@view-transitionsimply navigate normally. - Transition types exist for both models. For cross-document, you can declare them in the at-rule or set them with
event.viewTransition.typesinpagereveal. Check browser support before relying on them.
Common mistakes
- Adding
@view-transitionto only one page. Both pages need it. - Forgetting the
nullcheck onevent.viewTransition. The events fire on every navigation. - Assuming JavaScript variables survive. The old document is gone after navigation.
- Reusing a name for several list items. Duplicate names make the transition fail, as they do in the same-document case. Set the name only on the one item that was clicked.
- Expecting a transition across origins. It will not run.
Choosing between them
Use a same-document transition when your app already updates the DOM in JavaScript, such as in a single-page app or when toggling a panel. Use a cross-document transition when pages are separate HTML documents, typically server-rendered, and you want animation without adding a client-side router. You can use both in one site. A multi-page site can opt in to cross-document transitions and still use startViewTransition() for in-page changes.
Exercise
A multi-page site has @view-transition { navigation: auto; } on its list page only. The detail page has no such rule. A user clicks from the list to the detail page. What happens, and what is the fix?
Show answer
The navigation happens with no view transition, because both the old and the new page must opt in. The fix is to add the same @view-transition { navigation: auto; } rule to the detail page, for example through a shared stylesheet.
Exercise
In a cross-document shared-element transition, why do you set the view-transition-name in pageswap on the old page and in pagereveal on the new page, instead of inside one callback as in the same-document case?
Show answer
There is no update callback, and the two elements live in different documents. The old page must name its element before the old snapshot is captured. The new page must name its element before its first render so the new snapshot includes the name. Each page runs its own code at its own moment.