Sub topic 6
Morphing shared elements between pages
This chapter assumes you can already opt in with @view-transition { navigation: auto; } on both pages. It also assumes you know the default root crossfade and the ::view-transition-old() and ::view-transition-new() pseudo-elements.
What "shared" means
By default the browser snapshots the whole page as one image named root. The old and new pages crossfade. A shared element transition pulls one element out of that snapshot. You do this by giving it a view-transition-name.
At the start of the transition, the browser checks the old page and the new page for names:
- If a name exists on both pages, the browser treats the two elements as the same thing. It animates one group from the old element's position and size to the new element's position and size. While it does, it crossfades the old appearance into the new one.
- If a name exists on only one page, that element fades out (old page) or fades in (new page) on its own.
A static example
If one element on each page always carries the same name, plain CSS is enough. A site header and a post's hero image are typical cases. Put this in the stylesheet that both pages load:
@view-transition {
navigation: auto;
}
.site-header {
view-transition-name: site-header;
}
.post-hero img {
view-transition-name: post-image;
}
::view-transition-group(site-header) {
animation-duration: 0s;
}The header gets its own snapshot, so it no longer takes part in the root crossfade. It does not flicker while the content around it changes. Setting the group's duration to 0s makes it jump to its final state at once. This is fine here because the header looks the same on both pages.
Resizing images without stretching
The group animates the element's size. The old and new snapshots fill that group, so an image with a different aspect ratio on each page can look stretched mid-flight. Make the snapshots crop instead:
::view-transition-old(post-image),
::view-transition-new(post-image) {
height: 100%;
object-fit: cover;
}Rules that cause most failures
- Names must be unique within a page. If two rendered elements share a name, the transition is skipped and the navigation happens without animation. A list of cards cannot all be named
post-image. - Names are read when each page is captured. The old page is read just before it goes away. The new page is read when it is first rendered. Both pages must carry the name at those moments.
- The element must be rendered. An element with
display: nonehas no snapshot, so it cannot take part. noneis the default value. It means "do not name this element". It is not a valid custom name.
Naming elements also has a cost. Each named element is captured and animated as its own layer. Name only what the user should see move.
Exercise
A blog list page has twelve cards, each with a thumbnail styled view-transition-name: post-image. The post page has one hero image with the same name. Clicking a card gives a plain page change with no animation. Why, and what is the general fix?
Show answer
Twelve rendered elements on the list page share the name post-image. Names must be unique per page, so the transition is skipped. The fix is to give the name to only one thumbnail, the one the user clicked, and to do that at navigation time with JavaScript rather than in static CSS.