Sub topic 7
Building a shared-element transition
This chapter assumes you know how document.startViewTransition() works and what the ::view-transition pseudo-element tree contains. It also assumes you can write basic CSS and JavaScript for a single-page app. Here you will make one element appear to travel from a list card to a detail view.
The idea
A shared-element transition animates one element in the old state into one element in the new state. You mark both elements with the same view-transition-name. The browser then captures them as a separate pair of snapshots instead of part of the root snapshot. It animates position and size between the two.
The rule that causes most bugs is that a name must be unique among rendered elements at each capture. The old state may have one element with the name, and the new state may have one. Two at once cancels the transition.
Step 1: Name the old element
Give the clicked card the name hero before the browser captures the old state. The name goes on in your code, before you call startViewTransition().
Step 2: Move the name in the callback
Inside the callback, remove the name from the old card, update the DOM, and put the name on the new element. The browser captures the new state after the callback finishes.
function openDetail(card, id) {
if (!document.startViewTransition) {
showDetail(id);
return;
}
card.style.viewTransitionName = 'hero';
const transition = document.startViewTransition(() => {
card.style.viewTransitionName = '';
showDetail(id);
document.querySelector('.detail-image').style.viewTransitionName = 'hero';
});
transition.finished.finally(() => {
const detailImage = document.querySelector('.detail-image');
if (detailImage) detailImage.style.viewTransitionName = '';
});
}Removing the name from the card matters. If the card kept hero while .detail-image also had it, both would be rendered in the new state and the transition would be skipped. The finished handler clears the name afterward, so the detail image is free to take part in a later transition.
Step 3: Tune the motion
The browser builds a ::view-transition-group(hero) pseudo-element for the pair. It carries the position and size animation, so you change the movement by styling the group.
::view-transition-group(hero) {
animation-duration: 400ms;
animation-timing-function: ease-in-out;
}The pair inside the group, ::view-transition-old(hero) and ::view-transition-new(hero), cross-fades by default. That is usually what you want when the two images differ.
Common problems
- Nothing moves. Check that exactly one element has the name in each state. Duplicate names are the usual cause.
- The element jumps at the end. The name was removed too early or too late. The new element must have the name when the callback returns.
- The element is clipped or sits under other content. Snapshots render in the top layer, not inside the original parent. Overflow clipping on the original parent no longer applies, and stacking now follows the pseudo-element tree.
Exercise
A card list gives every card view-transition-name: hero in CSS, and clicking a card opens a detail view that also uses hero. The transition never plays. Why, and how do you fix it?
Show answer
Many elements share the name hero in the old state, so names are not unique and the browser skips the transition. Set the name only on the clicked card, in JavaScript, before calling startViewTransition(). Then move it to the detail image inside the callback.