Click a tile. It becomes the header of the next view and stays the same object on the
way there, so nobody loses their place. The browser snapshots both states and morphs between the
elements that share a view-transition-name; the only JavaScript is one call and one
name being set and unset.
Everything
/* 1. carry the clicked element, by name, for the length of the change */
function navigate(el, change){
if (!document.startViewTransition ||
matchMedia("(prefers-reduced-motion:reduce)").matches){ change(); return; }
el.style.viewTransitionName = "vt-hero"; /* unique, for one moment */
const t = document.startViewTransition(change);
t.finished.finally(() => { el.style.viewTransitionName = "" });
}
/* 2. style it. There is no step 3. */
::view-transition-group(vt-hero){
animation-duration: var(--m-dur-medium4);
animation-timing-function: var(--m-ease-emph-decelerate);
}
@media (prefers-reduced-motion:reduce){
::view-transition-group(*), ::view-transition-old(*),
::view-transition-new(*){ animation: none !important }
}/* in the stylesheet of BOTH pages, same origin */
@view-transition { navigation: auto }
/* give the element that continues the same name on both pages */
.hero{ view-transition-name: vt-hero }Printed, not demonstrated.
Cross-document transitions need two documents and this entry is one standalone file. It is
narrower than the same-document API: same origin, no cross-origin redirect, and the navigation has
to come from the page rather than from browser chrome. The pageswap and
pagereveal events are where you set a name that depends on which link was used.