Sub topic 1
Your first view transition with document.startViewTransition()
Prerequisites
This chapter assumes you know basic JavaScript, including promises. You should also know how to update the DOM in a single-page app, and how CSS selectors and pseudo-elements work. You don't need to know anything about view transitions yet.
What a view transition does
A same-document view transition animates between two states of one page. You tell the browser to start a transition and give it a function that changes the DOM. The browser captures the old view, runs your function, captures the new view, and then animates from one to the other.
By default, the animation is a cross-fade of the whole page. You get it without writing any CSS.
Wrapping an update
Call document.startViewTransition() and pass a callback that performs the DOM update. The callback is the only place where the DOM should change.
function updateView(nextId) {
document.querySelector('#content').innerHTML = renderItem(nextId);
}
function navigate(nextId) {
if (!document.startViewTransition) {
updateView(nextId);
return;
}
document.startViewTransition(() => updateView(nextId));
}renderItem stands for your own function that returns the HTML for an item. The if check handles browsers that don't have the API. In those browsers the update runs right away, with no animation, so the page still works.
What happens in each step
- The browser takes a snapshot of the current page. A snapshot is a static image of how an element looks at that moment.
- The page's rendering is paused, so the user does not see the DOM change.
- Your callback runs. If it returns a promise, the browser waits for it to settle.
- The browser captures the new state as a live view.
- The browser builds a tree of pseudo-elements, which are CSS-addressable parts that hold the old and new images, and plays the animation.
Because rendering is paused during the update, a slow callback freezes what the user sees. Keep the callback short and do slow work, such as fetching data, before you call startViewTransition() when you can.
Common mistakes
- Changing the DOM outside the callback. The browser captures the old state before the callback runs, so an update made earlier appears in the snapshot of the old view and nothing animates.
- Skipping the feature check. Calling
document.startViewTransitionwhere it is undefined throws aTypeError. - Doing slow work inside the callback. The page looks frozen until the callback finishes.
Exercise
A developer writes updateView(nextId); document.startViewTransition(); and sees no animation. What is wrong, and how do they fix it?
Show answer
The DOM changes before the transition starts, so the old and new snapshots look the same and there is nothing to animate. Move the update into the callback: document.startViewTransition(() => updateView(nextId));.
Exercise
Why does the example check document.startViewTransition before calling it?
Show answer
Browsers without the API would throw a TypeError. The check runs the update directly in those browsers, so the page works without animation.