completefrontend
Open lab My progress
THE WEB PERFORMANCE FIELD GUIDE

View transitions and performance

What a view transition costs: snapshots, paused rendering and pseudo-element animations. How to keep it short, and when to skip it.

By the end, you can
  • 01
    Read what a transition costsKnow when Chrome takes its snapshots, why the screen freezes while the update callback runs, and what each view-transition-name adds.
  • 02
    Keep a transition fastDo slow work before startViewTransition(), name only the elements that need it, and keep the animation short.
  • 03
    Know when to skip itRespect prefers-reduced-motion, and know what a cross-document transition costs the next page.

The leaf stopped too

Asha is back on Leaf and Kettle's mint tea reviews. In lessons 32.1 and 32.2, the small leaf next to the title kept swaying. It swayed even while Ravi's ranking code kept the page busy.

Ravi has added a new button: Most helpful first. It runs his ranking code, then puts the 1,000 reviews in their new order. He wanted the change to look smooth, so he wrapped it in a view transition. A view transition animates a page from how it looked before a change to how it looks after. For how the API works, see View transitions in the React course.

Asha taps the button. Nothing happens. The leaf stops too, in the middle of its sway. Almost a second later, the new order fades in. Here is what she saw, in real frames from Chrome:

Asha's phone in five real frames from Chrome, at 0 ms, 300 ms, 700 ms, 1,000 ms, 1,320 ms after her tap: her tap, no change, no change, fading in, the new order. Under them, Ravi's update callback ran from 116 ms to 597 ms, Chrome paused rendering from 115 ms to 788 ms, and one thin bar marks every frame Chrome showed. There is no frame from 136 ms to 936 ms.0 msAsha's phone 0 ms after her tap: her tap.her tap300 msAsha's phone 300 ms after her tap: no change.no change700 msAsha's phone 700 ms after her tap: no change.no change1,000 msAsha's phone 1,000 ms after her tap: fading in.fading in1,320 msAsha's phone 1,320 ms after her tap: the new order.the new orderthe update callbackrendering pausedno new frame for 800 mstap, 0 ms1,400 ms
Real frames from Chrome, and one thin bar for every frame it showed. For 800 ms, there was no new frame at all, not even for the swaying leaf. Then the new order faded in.

Here are the main numbers as code:

Asha's tap on Most helpful firstPop out in the code editor (opens in a new tab)JavaScript
// Asha taps "Most helpful first". From Chrome's trace, in ms after her tap.const callback = { from: 115.9, to: 596.6 }; // Ravi's update callbackconst noNewFrame = { from: 135.5, to: 935.5 }; // the screen didn't change at allconst listChanged = 952.2; // the first frame with the new order in it console.log(`The update callback ran for ${(callback.to - callback.from).toFixed(1)} ms.`);console.log(`No new frame for ${noNewFrame.to - noNewFrame.from} ms.`);console.log(`The list first changed at ${listChanged} ms.`);
  • Line 1 is a comment. A trace is Chrome's detailed record of what it did, as in lesson 7.2. A millisecond (ms) is a thousandth of a second.
  • Line 2 holds when Ravi's update callback started and ended. That's the function he gave to startViewTransition(). It changes the page.
  • Line 3 holds the stretch when Chrome showed no new frame at all. A frame is one picture on the screen.
  • Line 4 holds the first frame with the new order in it.
  • Line 6 prints The update callback ran for 480.7 ms. Line 7 prints No new frame for 800 ms.
  • Line 8 prints The list first changed at 952.2 ms.

So for 800 ms, nothing on the screen changed. Not the list, and not the leaf. In lesson 32.1, the compositor kept the leaf swaying through the same ranking code. What's different now?

A view transition works in a fixed order:

  1. Chrome takes a picture of the page as it is. This picture is called a snapshot.
  2. Chrome pauses rendering. It stops drawing new frames.
  3. Chrome runs the update callback, which changes the page.
  4. When the callback is done, Chrome looks at the new page and starts drawing again.
  5. It animates from the old picture to the new page.

The pause keeps the old picture on screen while the page changes behind it. And Chrome pauses the compositor's frames too. Its source says pausing rendering "pauses both main and impl frames to ensure no visual updates are presented." Impl frames are the compositor's own frames. That's why the leaf stopped.

Real-life analogyA painted curtain while the stage is reset

At a theatre, the crew changes the set between scenes. First a painted curtain comes down. It shows the last scene, so the audience doesn't watch the crew at work. When the set is ready, the curtain fades away. A quick crew means a short wait. A slow crew means the audience stares at a painting.

In real life: The painted curtain
In JavaScript: The old snapshot, ::view-transition-old
In real life: Lowering the curtain
In JavaScript: Chrome pausing rendering
In real life: The crew changing the set
In JavaScript: The update callback
In real life: The curtain fading away
In JavaScript: The animation to the new page
In real life: A slow crew
In JavaScript: Ravi's ranking code inside the callback
The main idea

A view transition photographs the page, pauses rendering while your update callback runs, then animates from the old picture to the new page. Everything inside the callback is time the visitor stares at a still picture. So keep the callback short, name few elements, keep the animation short, and skip it for people who ask for less motion.

This lesson takes each cost in turn: the snapshots, the update callback, moving between pages, the animation, and reduced motion.

How the course recorded it

  • The browser: Chrome 157, on a screen the size of Asha's phone, 412 by 823 px. Its CPU was slowed 4 times, as in lessons 32.1 and 32.2. That slows the main thread only.
  • The page: Ravi's page from lesson 32.2, with 1,000 reviews and the leaf. A new row under the title holds the button and a link to the Mint tea page in the shop. The ranking code is the same stand-in as in lesson 32.1: 400 ms of work. Each version changes one thing.
  • The taps: a real mouse click on the button, on a freshly loaded page. Five taps per version, taking turns. In the first round, Chrome's GPU process did extra one-time work in some transitions. So the numbers come from the other four taps: the slower of the two middle ones.
  • The frames: Chrome's own screenshot of every frame it showed, and its view transition events in the trace. Your own times will differ.
  • The visits: a click on the link to the Mint tea page, five per version. The tool that drives Chrome turns off paint holding. The course turned it back on, as in a normal Chrome. Paint holding keeps the old page on screen until the new one paints.

Snapshots and the transition pseudo-elements

Here is Ravi's first click handler:

Ravi's first view transitionJavaScript
document.querySelector("#sort").addEventListener("click", () => {  document.startViewTransition(() => {    const order = rankReviews();    list.append(...order);  });});
  • Line 1 listens for clicks on the button.
  • Line 2 starts a view transition. Its argument is the update callback.
  • Line 3 runs the ranking code and gets the new order. Line 4 moves the cards into it.

The CSS View Transitions spec says what happens next. First, Chrome captures the old state. For every element with a view-transition-name, it takes a snapshot of that element. The page itself always has a name: root.

Then the spec turns on rendering suppression: no new frames. During it, every tap goes to the html element. Chrome runs the update callback in a task of its own. When the callback's promise settles, the pause ends and Chrome captures the new state.

A snapshot isn't taken with a camera. Chrome marks parts of a frame it draws anyway, and its GPU process keeps a copy. The new state isn't a copy at all. Chrome's team puts it this way: "the new snapshot is not a screenshot but actually a live representation".

Last, Chrome builds a tree of pseudo-elements: parts of the page that CSS can style, but that aren't in your HTML. Here is the tree for Ravi's page:

The pseudo-elements of Ravi's transitionText
::view-transition
└─ ::view-transition-group(root)
   └─ ::view-transition-image-pair(root)
      ├─ ::view-transition-old(root)
      └─ ::view-transition-new(root)
  • Line 1, ::view-transition, sits on top of everything and covers the screen.
  • Line 2, the group, moves and resizes from the old place to the new one.
  • Line 3, the image pair, holds the two pictures together.
  • Line 4 is the old snapshot. It fades out. Line 5 is the live new page. It fades in.

The spec's style sheet gives every group a quarter-second animation: animation-duration: 0.25s. Chrome's trace showed 5 animations on Ravi's page: the group's move, the two fades, and two that blend the pictures. Chrome reported no reason the two fades couldn't run on the compositor. For the other three, it gave the reason "Animation has no visible change": their values stay the same.

Every name costs

Ravi wanted the cards to glide to their new places, not just fade. A card moves on its own only if it has its own name. So he gave every card one:

A name for every cardJavaScript
for (const card of list.children) {
  card.style.viewTransitionName = card.id;
}

Line 1 goes through all 1,000 cards. Line 2 gives each card its id as its name, like review-1.

Each name means one more snapshot, one more group of four pseudo-elements, and five more animations. Chrome captures every named element that the page lays out, on screen or not.

With 1,000 names, the course watched for 10 seconds. The list never changed. Chrome's trace showed one Recalculate style step that began 476 ms after the tap, and hadn't ended. The transition never got as far as its snapshot.

So Ravi named only the cards near the top: the first four before the sort, and the first four after it.

Names for a few cards, only during the transitionJavaScript
document.querySelector("#sort").addEventListener("click", () => {  const order = rankReviews();  const seen = [...list.children].slice(0, 4).concat(order.slice(0, 4));  for (const card of seen) card.style.viewTransitionName = card.id;  const transition = document.startViewTransition(() => list.append(...order));  transition.finished.then(() => {    for (const card of seen) card.style.viewTransitionName = "";  });});
  • Line 2 runs the ranking code, before the transition.
  • Line 3 picks the first four cards of the old order and of the new one. Three cards fit on Asha's screen, so four covers them.
  • Line 4 names them. Line 5 starts the transition, whose update callback only moves the cards.
  • Lines 6 to 8 take the names away when the animation ends.
One tap on three versions of Ravi's page. no card named: 1 group, 5 animations, +37.1 MB GPU, list at 925 ms. 8 cards named: 9 groups, 45 animations, +54.3 MB GPU, list at 957 ms. all 1,000 named: 1,001 names, no animation, not measured, no change in 10 s.no card named1 group5 animations+37.1 MB GPUlist at 925 ms8 cards named9 groups45 animations+54.3 MB GPUlist at 957 msall 1,000 named1,001 namesno animationnot measuredno change in 10 s
Every name is one more group of pictures for Chrome to make and animate. With all 1,000 cards named, the list hadn't changed 10 seconds after the tap.

The course measured both versions:

What eight names costPop out in the code editor (opens in a new tab)JavaScript
// One tap each, held still once the animation began. From Chrome.const versions = [  { named: "the page only", animations: 5, gpuMbMore: 37.1 },  { named: "the page and 8 cards", animations: 45, gpuMbMore: 54.3 },];for (const v of versions) {  console.log(`${v.named}: ${v.animations} animations, ${v.gpuMbMore} MB more`);}
  • Lines 3 and 4 hold the animations each transition started, and its extra GPU memory.
  • Line 7 prints the page only: 5 animations, 37.1 MB more and the page and 8 cards: 45 animations, 54.3 MB more.

The memory is the GPU process's, while the transition was on screen. The course held each transition still once its animation began. Then it asked Chrome for a memory dump. Eight cards cost about 17 MB more.

Chrome's source limits each capture to twice the screen's longer side. More "would significantly add to the memory overhead". Why it matters: name only the elements that must move on their own. Name them just before the transition, and take the names away after it.

Real-life analogyOne photo of the room, or a portrait of every guest

At a party, a photographer can take one photo of the whole room. Or she can take a portrait of every guest, so each one can go anywhere in the album later. A few portraits are quick. A thousand keep everyone waiting, and fill the album.

In real life: The photo of the whole room
In JavaScript: The root snapshot
In real life: A portrait of one guest
In JavaScript: A snapshot of one named element
In real life: Placing a portrait somewhere new in the album
In JavaScript: A group animating one card to its new place
In real life: A thousand guests waiting in line
In JavaScript: 1,000 names: no change within 10 seconds
Playground: how many names
How the cards get their namesJavaScript
document.querySelector("#sort").addEventListener("click", () => {
  const order = rankReviews();
  document.startViewTransition(() => list.append(...order));
});
No card namedRecorded
The list first changed925 ms
Chrome paused rendering for242.7 ms
Animations the transition started5
GPU memory while it was on screen37.1 MB more
Try it yourself
No card named

Only the page itself is captured: one snapshot, one group, and its animations.

Recorded in Chrome 157 with the main thread 4 times slower, the ranking code before startViewTransition(). The times are from the middle of 4 taps. The memory is from one more tap, held still once the animation began.

Keep the update callback short

In Ravi's first version, the ranking code ran inside the update callback. So it ran while rendering was paused. What if it ran before startViewTransition()?

The ranking code firstJavaScript
document.querySelector("#sort").addEventListener("click", () => {  const order = rankReviews();  document.startViewTransition(() => list.append(...order));});

Line 2 runs the ranking code first, while Chrome still draws frames. Line 3 starts the transition. Its update callback only moves the cards.

The course tapped three versions: Ravi's first, this one, and the page with no transition at all.

Three versions of the buttonPop out in the code editor (opens in a new tab)JavaScript
// Middle of four taps each. Times in ms after the tap.const taps = [  { version: "no transition", listChanged: 757.2, eventTiming: 768 },  { version: "ranking inside the callback", listChanged: 952.2, eventTiming: 136 },  { version: "ranking before it", listChanged: 925, eventTiming: 560 },];for (const t of taps) {  console.log(`${t.version}: ${t.listChanged} ms, Event Timing ${t.eventTiming} ms`);}
  • Lines 3 to 5 hold when the list first changed, and the tap's Event Timing duration. Event Timing is the API behind INP, from lesson 26.1.
  • Line 8 prints no transition: 757.2 ms, Event Timing 768 ms and ranking inside the callback: 952.2 ms, Event Timing 136 ms.
  • Then ranking before it: 925 ms, Event Timing 560 ms.
The same tap on three versions of Ravi's page, middle of four taps each. No transition: no pause, the longest stretch with no new frame was 17 ms, and the list changed at 757 ms. Ranking inside the update callback: Chrome paused rendering from 115 ms to 788 ms, the longest stretch with no new frame was 800 ms, and the list changed at 952 ms. Ranking before startViewTransition(): Chrome paused rendering from 524 ms to 767 ms, the longest stretch with no new frame was 350 ms, and the list changed at 925 ms.Thin bars: frames Chrome showed. Shaded: rendering paused.No transitionlist changed at 757 msRanking inside the update callbacklist changed at 952 msRanking before startViewTransition()list changed at 925 mstap, 0 ms1,200 ms
Each thin bar is a frame Chrome showed. The shaded band is the time Chrome paused rendering. With the ranking code inside the update callback, no frame came for 800 ms.

Look at the frames first. With the ranking code inside the callback, Chrome showed no new frame for 800 ms. With it before the transition, the leaf kept swaying through the ranking code. The longest stretch with no new frame shrank to 350 ms: the snapshots and the new state.

The list changed at about the same time either way. The transition itself added about 170 to 200 ms over the page with none. That's the snapshot's frame, the new state, and the animation's first frame.

Now the Event Timing numbers. The Event Timing spec ends a tap's duration at the next frame after it, as lesson 28.2 showed. With the ranking code inside the callback, that next frame was the snapshot's frame. It looked exactly like the frame before the tap.

So Event Timing measured 136 ms, while Asha waited 952.2 ms for anything to change. Why it matters: INP can't see time spent inside an update callback. When you add a view transition, measure when the new state appears, not just INP.

Chrome's docs give the same advice: "network fetches should be done before calling .startViewTransition(), while the page is still fully interactive". Why it matters: do slow work first. Then start the transition with a callback that only changes the DOM.

Playground: where the ranking code runs
Ravi's click handlerJavaScript
document.querySelector("#sort").addEventListener("click", () => {
  document.startViewTransition(() => {
    const order = rankReviews();
    list.append(...order);
  });
});
Ranking inside the callbackRecorded
The list first changed952.2 ms
All 4 taps931.4 ms, 952.2 ms, 922.8 ms, 985.5 ms
Longest stretch with no new frame800 ms
Chrome paused rendering for672.9 ms
The tap's Event Timing duration136 ms
Frames drawn while it animated17
Try it yourself
Ranking inside the callback: the list changed at 952.2 ms

Chrome took the snapshot, then paused rendering while the ranking code ran. The screen didn't change at all, and Event Timing ended at the snapshot's frame.

Recorded in Chrome 157 with the main thread 4 times slower. Each number is from the middle of 4 taps. Times are in ms after the tap.

When the update never ends

The spec lets a browser give up on a slow update callback after a time of its own choosing. In Chrome, that's 4 seconds: lines 1403 to 1407 of view_transition.cc. Ravi tried a version whose callback waits for the server. The course made the server take 6 seconds:

Waiting for the server inside the callbackJavaScript
document.querySelector("#sort").addEventListener("click", () => {  document.startViewTransition(async () => {    await fetch("votes.json"); // the server takes 6 seconds    list.append(...rankReviews());  });});
  • Line 2 starts the transition with an async update callback. Its promise settles when the function ends.
  • Line 3 waits for the server. Line 4 moves the cards when the answer comes.
What the 6-second wait didPop out in the code editor (opens in a new tab)JavaScript
// One tap; the server answered after 6 seconds. ms after the tap.const paused = { from: 141.7, to: 4142.5 };const ready = { rejectedAt: 4143.5, error: "TimeoutError" };const listChanged = 6614.3; console.log(`Rendering paused for ${(paused.to - paused.from).toFixed(1)} ms.`);console.log(`ready failed at ${ready.rejectedAt} ms with ${ready.error}.`);console.log(`The list changed at ${listChanged} ms, with no animation.`);
  • Line 6 prints Rendering paused for 4000.8 ms.
  • Line 7 prints ready failed at 4143.5 ms with TimeoutError.
  • Line 8 prints The list changed at 6614.3 ms, with no animation.

For four seconds, Asha's screen showed nothing new. Then Chrome gave up and skipped the transition. Its ready promise failed with a TimeoutError. When the answer came, the callback still ran and changed the list, with no animation. Why it matters: a slow update costs up to 4 seconds of a frozen screen, and then you lose the animation anyway.

Try it in your own browser. Pick where 500 ms of work runs, then tap the button in the small page:

Playground: the update callback in your browser
The click handlerJavaScript
sort.addEventListener("click", () => {
  document.startViewTransition(() => {
    work(500);
    reverseList();
  });
});
Inside the callbackLive
Try it yourself
Tap Most helpful first, and watch the leaf and the list

The tap runs 500 ms of work, then reverses the list. The page says when the animation began, and what Event Timing measured for your tap.

The small page below runs in this browser, in a frame of its own. The work keeps this whole tab busy, so the lesson page may freeze too. If you ask for less motion, the page skips the animation.

Same-document vs cross-document

So far, everything happened inside one page. The page called startViewTransition() and changed itself. That's a same-document transition.

Ravi wants one more. When Asha opens the Mint tea page from the reviews, the old page should fade into the new one. That's a cross-document transition: from one page load to the next. There's no function to call. Both pages opt in with a CSS rule:

The opt-in, on both pagesCSS
@view-transition {
  navigation: auto;
}

Lines 1 to 3 are an @view-transition rule. Line 2, navigation: auto, asks for a transition on navigations to and from this page. The Level 2 spec lists the other conditions:

  • Both pages are on the same origin.
  • The visitor starts the navigation, with a link, a form, or back and forward. Typing an address doesn't count.
  • The page stays visible the whole time.

Two events let each page take part. pageswap fires on the old page "at the last moment before a document is about to be unloaded". pagereveal fires on the new page "right before presenting the first frame". In the recording, both events had a viewTransition object when both pages opted in.

Real-life analogyTwo slide projectors and a dissolve

In an old slide show, two projectors point at one screen. The first one shows the last slide. When the next slide is in the second projector, the operator dissolves from one to the other. Both projectors must be set up for it. And the dissolve can't start until the next slide is in place.

In real life: The first projector, still showing its slide
In JavaScript: The old page, with its snapshot
In real life: The second projector
In JavaScript: The new page
In real life: Setting up both projectors for a dissolve
In JavaScript: @view-transition on both pages
In real life: Waiting for the next slide to drop into place
In JavaScript: The new page loading
In real life: The dissolve
In JavaScript: The cross-fade

The course clicked the link to the Mint tea page, with and without the opt-in:

Two ways to the Mint tea pagePop out in the code editor (opens in a new tab)JavaScript
// From the reviews page to the Mint tea page. Middle of four visits each.const visits = [  { version: "no transition", firstPaint: 316.1, fullyShown: 330.7 },  { version: "a cross-document transition", firstPaint: 442.4, fullyShown: 657.5 },];for (const v of visits) {  console.log(`${v.version}: first paint ${v.firstPaint} ms, fully shown ${v.fullyShown} ms`);}
  • Lines 3 and 4 hold the Mint tea page's first contentful paint, from Chrome's trace. Its largest contentful paint, the photo, came at the same moment.
  • The other number is the first frame that matched the finished page.
  • Line 7 prints no transition: first paint 316.1 ms, fully shown 330.7 ms and a cross-document transition: first paint 442.4 ms, fully shown 657.5 ms.
Asha's phone in five real frames from Chrome, at 0 ms, 200 ms, 490 ms, 560 ms, 710 ms after she clicked the link to the Mint tea page: her click, the reviews, fading in, fading in, the Mint tea page. Under them, two visits. No transition: first paint at 316 ms, fully shown at 331 ms. Cross-document transition: first paint at 442 ms, fully shown at 658 ms.0 msAsha's phone 0 ms after her click: her click.her click200 msAsha's phone 200 ms after her click: the reviews.the reviews490 msAsha's phone 490 ms after her click: fading in.fading in560 msAsha's phone 560 ms after her click: fading in.fading in710 msAsha's phone 710 ms after her click: the Mint tea page.the Mint tea pageNo transitionfully shown at 331 msCross-document transitionfully shown at 658 msclick, 0 ms800 ms
Real frames of the visit with a transition. The shaded part of each bar is the wait before the Mint tea page's first paint. The white part is the fade.

The server answered both visits after about 270 ms. Without a transition, the Mint tea page painted soon after. With one, the old page first had to take its snapshot. On Ravi's heavy page, slowed 4 times, that took one more frame. So the first paint came 126 ms later. Then the fade took more than 200 ms.

Chrome reported the largest contentful paint at 442.4 ms too, when the fade had only just begun. Why it matters: a cross-document transition delays the next page's first paint, and LCP doesn't count the fade. Keep the old page light, and the fade short.

For its first frame, the new page waits for its render-blocking files, like the style sheets in its head. The Level 2 spec warns that "overusing the render-blocking mechanism could make it so that the old state remains frozen for a long time". Chrome's docs say a navigation that takes more than four seconds skips the transition.

Read the table across: each row is one question, for each kind of transition.

Same-document and cross-document transitions
QuestionSame-documentCross-document
How does it start?The page calls startViewTransition().The visitor follows a link, or goes back or forward.
Who opts in?The page, by calling the function.Both pages, with @view-transition.
What does the visitor wait for?The update callback.The next page, up to its first frame.
How long does Chrome wait?4 seconds.4 seconds.
Where can you change it?The ViewTransition object it returns.The pageswap and pagereveal events.

Keeping transitions short

The animation runs after the pause. How long should it be? The default is a quarter of a second. Ravi tried a full second:

A one-second transitionCSS
::view-transition-group(*) {
  animation-duration: 1s;
}

Line 1 selects every group. Line 2 sets its animation to one second. The old and new pictures take their duration from the group.

From ready to finishedPop out in the code editor (opens in a new tab)JavaScript
// From ready to finished, middle of four taps each, in ms.const animations = [  { css: "the default", ready: 804.2, finished: 1197.8 },  { css: "animation-duration: 1s", ready: 775.7, finished: 1915.3 },];for (const a of animations) {  console.log(`${a.css}: ${(a.finished - a.ready).toFixed(1)} ms`);}
  • Lines 3 and 4 hold when ready and finished settled. ready settles as the animation is about to start. finished settles when the new page is "fully visible and interactive".
  • Line 7 prints the default: 393.6 ms and animation-duration: 1s: 1139.6 ms.

Both took longer than their animation. The animation's first frame needs the main thread, and Ravi's page is heavy. Does a longer animation matter, if it's smooth? The course clicked the middle of Asha's screen 100 ms after ready, and again after finished:

What a click reachedPop out in the code editor (opens in a new tab)JavaScript
// A click in the middle of Asha's screen. What did it reach?const clicks = [  { when: "100 ms into the animation", target: "html" },  { when: "after finished", target: "h3.review-name in #review-59" },];for (const c of clicks) console.log(`${c.when}: ${c.target}`);
  • Lines 3 and 4 hold where each click landed. The target is the element the click event went to.
  • Line 6 prints 100 ms into the animation: html and after finished: h3.review-name in #review-59.

During the transition, the click reached the html element, not the review under it. The pseudo-elements cover the screen. The spec says the captured elements "do not respond to hit-testing" while it animates. After it, the same click reached a review's name. Why it matters: every millisecond of animation is a millisecond when taps miss your page. Keep it short.

Real-life analogyThe gates at a level crossing

Where a railway crosses a road, gates come down while a train passes. Nobody can cross, even when the road beyond is empty. A short train means a short wait. A long, slow train holds everyone back.

In real life: The gates coming down
In JavaScript: ::view-transition covering the screen
In real life: The train passing
In JavaScript: The animation playing
In real life: Someone waiting to cross
In JavaScript: Asha's tap reaching html
In real life: A short train
In JavaScript: The default quarter second

A busy main thread at the start

Lesson 32.1 showed that a compositor animation keeps going while the main thread is busy. Chrome reported no reason the fades and the cards' moves couldn't run on the compositor. Yet in the recording, the animation waited for the main thread before it started. Ravi's busy version adds one line to the eight-card handler:

Other work, as soon as the transition is readyJavaScript
transition.ready.then(saveChoice);

When ready settles, the line runs saveChoice. That stands in for other work: 400 ms on the main thread.

The first new frame, with and without the workPop out in the code editor (opens in a new tab)JavaScript
// The first frame with the new order, middle of four taps each, in ms.const taps = [  { version: "8 named cards", listChanged: 957 },  { version: "8 named cards, 400 ms of work at ready", listChanged: 1336.8 },];for (const t of taps) console.log(`${t.version}: ${t.listChanged} ms`);
  • Lines 3 and 4 hold when the new order first reached the screen.
  • Line 6 prints 8 named cards: 957 ms and 8 named cards, 400 ms of work at ready: 1336.8 ms.

The animation didn't freeze half-way. It hadn't started. No new frame came for 766.6 ms. Then it played the same frames as the version without the work, about 380 ms later. Why it matters: keep the main thread free right after ready, or the animation waits.

Reduced motion

Some people feel dizzy or sick when things move on a screen. Others find it hard to focus. They can ask their system for less motion. The Media Queries spec shows that wish to CSS as prefers-reduced-motion. Its value is reduce for motion that can "trigger discomfort for those with vestibular motion sensitivity". The vestibular system is the inner ear's sense of balance.

The first way to respect it is CSS that turns the transition's animations off:

No animation for less motionCSS
@media (prefers-reduced-motion: reduce) {
  ::view-transition-group(*),
  ::view-transition-old(*),
  ::view-transition-new(*) {
    animation: none;
  }
}
  • Line 1 applies the rule only when the visitor asks for less motion.
  • Lines 2 to 4 select every group, every old picture and every new one.
  • Line 5 removes their animations.

The second way skips the transition in the script. Ravi adds one line to his handler, right after the ranking code:

No transition for less motionJavaScript
if (matchMedia("(prefers-reduced-motion: reduce)").matches) return list.append(...order);

matchMedia() checks a media query from JavaScript. Its matches is true when Asha asks for less motion. Then the line moves the cards and returns, before any name or transition.

The course told Chrome that Asha asks for less motion, with DevTools' Emulation.setEmulatedMedia. Then it tapped both versions:

Two ways to respect less motionPop out in the code editor (opens in a new tab)JavaScript
// Asha asked for less motion. First frame with the new order, in ms.const taps = [  { version: "CSS turns the animations off", listChanged: 909.1, paused: 250.3 },  { version: "the script skips the transition", listChanged: 710.7, paused: 0 },];for (const t of taps) console.log(`${t.version}: ${t.listChanged} ms, paused ${t.paused} ms`);
  • Lines 3 and 4 hold when the new order reached the screen, and how long Chrome paused rendering.
  • Line 6 prints CSS turns the animations off: 909.1 ms, paused 250.3 ms and the script skips the transition: 710.7 ms, paused 0 ms.

With the CSS, there was no animation. But Chrome still took both snapshots, and still paused rendering. The script way skipped all of that. The new order arrived about 200 ms sooner. Why it matters: when a visitor asks for less motion, don't start the transition at all.

For a cross-document transition, put the opt-in inside a media query:

The opt-in, only without a wish for less motionCSS
@media (prefers-reduced-motion: no-preference) {
  @view-transition {
    navigation: auto;
  }
}

Line 1 applies the rule only for visitors with no preference. Lines 2 to 4 are the opt-in. In the recording, a visitor asking for less motion got no transition. pagereveal had no viewTransition, and the Mint tea page showed at 330 ms.

Less motion doesn't have to mean none. Chrome's docs point out that reduced motion "doesn't mean the user wants no motion". A quick, gentle fade can still show what changed.

Real-life analogyA note that says: please knock softly

Some people put a note on their door: "Baby sleeping. Please knock softly." A kind courier doesn't ring the loud bell. He knocks gently, or leaves the parcel by the door. He doesn't ring the bell and then whisper sorry.

In real life: The note on the door
In JavaScript: prefers-reduced-motion: reduce
In real life: Ringing the loud bell
In JavaScript: A big sliding transition
In real life: Knocking softly
In JavaScript: A short, gentle fade
In real life: Leaving the parcel by the door
In JavaScript: Skipping the transition in the script
In real life: Ringing anyway, then whispering
In JavaScript: animation: none, after the snapshots

Does Chrome pause rendering for it?

For each one, ask: does it run while Chrome draws no frames, or while frames keep coming?

Does Chrome pause rendering for it?
  • 400 ms of ranking code inside the update callback
  • Waiting for fetch() inside the update callback
  • 400 ms of ranking code before startViewTransition()
  • A view-transition-name on all 1,000 cards
  • animation: none on the pseudo-elements, for less motion
  • Checking prefers-reduced-motion and updating the list directly
  • animation-duration: 1s on ::view-transition-group(*)
Try it yourself
0 of 7 correct

Sort each one by what happens on Asha's screen while it runs.

Choose a category for every card. You can change an answer at any time; Reset clears them all.

Then pick what to do. Read the table across: what you want, what to do, and why.

Keeping a view transition fast
If you wantDo thisWhy
To animate a change on the page.Do slow work first. Let the update callback only change the DOM.Chrome pauses rendering while the callback runs.
Some cards to move on their own.Name only those cards, just before the transition.Each name is a snapshot, a group and five animations.
A quick animation.Keep the quarter second, and keep the main thread free at ready.Taps reach html while it plays, and it can't start while the main thread is busy.
Less motion for those who ask.Check prefers-reduced-motion and skip startViewTransition().animation: none still pays for the snapshots.
A transition between pages.Opt in on both pages, and keep the old page light.The old page's snapshot comes before the new page's first paint.

Common mix-ups

  • "A view transition runs on the compositor, so it can't freeze the page." No. While Ravi's update callback ran, Chrome showed no new frame for 800 ms, not even for the leaf.
  • "A good INP means the transition felt fast." No. Event Timing measured 136 ms. The list changed at 952.2 ms.
  • "A view-transition-name is free, so name everything." No. With 1,000 names, the list didn't change within 10 seconds.
  • "The browser waits for the update callback as long as it takes." No. Chrome gives up after 4 seconds and skips the animation. The change still happens, later.
  • "animation: none skips the work." No. Chrome still took both snapshots and paused rendering for 250.3 ms.
  • "A cross-document transition costs nothing until the next page is ready." No. The old page takes its snapshot first. The first paint moved from 316.1 to 442.4 ms.
  • "The page stays clickable during the animation." No. A click in the middle of the screen reached html.

Practice exercises

Answer first, then use the hints.

Exercise 1 · Warm-upCount the animations

This page gives three paragraphs a name each, then starts a view transition. What does line 4 print, in Chrome?

Starter codePop out in the code editor (opens in a new tab)JavaScript
document.body.innerHTML = '<p id="a">A</p><p id="b">B</p><p id="c">C</p>';
for (const p of document.querySelectorAll("p")) p.style.viewTransitionName = p.id;
const transition = document.startViewTransition(() => document.body.append("Done"));
transition.ready.then(() => console.log(document.getAnimations().length));

Answer, then press Check. Spacing and letter case don’t matter.

    Exercise 2 · Warm-upWhich runs first?

    What does the second line of this page print?

    Starter codePop out in the code editor (opens in a new tab)JavaScript
    const transition = document.startViewTransition(() => console.log("update"));
    transition.finished.then(() => console.log("finished"));
    transition.ready.then(() => console.log("ready"));
    transition.updateCallbackDone.then(() => console.log("updateCallbackDone"));
    console.log("called");

    Answer, then press Check. Spacing and letter case don’t matter.

      Exercise 3 · PracticeEight names in the playground

      In the how many names playground, how long did Chrome pause rendering with 8 cards named?

      Answer, then press Check. Spacing and letter case don’t matter.

        Exercise 4 · PracticeWaiting for the server

        Ravi gets the new order from his server, which takes 2 seconds. For about how long does Asha's screen show no new frame? Then fix it.

        Starter codePop out in the code editor (opens in a new tab)JavaScript
        document.querySelector("#sort").addEventListener("click", () => {
          document.startViewTransition(async () => {
            const response = await fetch("order.json"); // the server takes 2 seconds
            const ids = await response.json();
            list.append(...ids.map((id) => document.getElementById(id)));
          });
        });

        Answer, then press Check. Spacing and letter case don’t matter.

          Exercise 5 · ChallengeThe timeout's message

          Open the course's copy of Ravi's first page, and run these lines in DevTools' Console. What message do they print?

          Starter codePop out in the code editor (opens in a new tab)JavaScript
          document.startViewTransition(() => new Promise(() => {}))
            .ready.catch((error) => console.log(error.message));

          Answer, then press Check. Spacing and letter case don’t matter.

            Exercise 6 · ChallengeWait for an element

            A cross-document transition can hold the new page's first frame until one of its elements exists. Which rel value does the <link> for that use?

            Answer, then press Check. Spacing and letter case don’t matter.

              Check your understanding

              For each question, ask: when does Chrome take its pictures, and when does it stop drawing frames?

              The view transitions quiz · 8 questionsScore: first tries count
              1. Question 1 of 8What does the last line of Asha's tap example print?

                Read the code, then predictPop out in the code editor (opens in a new tab)JavaScript
                // Asha taps "Most helpful first". From Chrome's trace, in ms after her tap.
                const callback = { from: 115.9, to: 596.6 }; // Ravi's update callback
                const noNewFrame = { from: 135.5, to: 935.5 }; // the screen didn't change at all
                const listChanged = 952.2; // the first frame with the new order in it
                
                console.log(`The update callback ran for ${(callback.to - callback.from).toFixed(1)} ms.`);
                console.log(`No new frame for ${noNewFrame.to - noNewFrame.from} ms.`);
                console.log(`The list first changed at ${listChanged} ms.`);

                Choose an answer to see the explanation.

              2. Question 2 of 8In lesson 32.1 the leaf kept swaying through Ravi's ranking code. Why did it stop this time?

                Choose an answer to see the explanation.

              3. Question 3 of 8What does the second line of the three-versions example print?

                Read the code, then predictPop out in the code editor (opens in a new tab)JavaScript
                // Middle of four taps each. Times in ms after the tap.
                const taps = [
                  { version: "no transition", listChanged: 757.2, eventTiming: 768 },
                  { version: "ranking inside the callback", listChanged: 952.2, eventTiming: 136 },
                  { version: "ranking before it", listChanged: 925, eventTiming: 560 },
                ];
                for (const t of taps) {
                  console.log(`${t.version}: ${t.listChanged} ms, Event Timing ${t.eventTiming} ms`);
                }

                Choose an answer to see the explanation.

              4. Question 4 of 8Event Timing measured Asha's tap at 136 ms, but the list changed at 952.2 ms. Why so different?

                Choose an answer to see the explanation.

              5. Question 5 of 8What does each view-transition-name add to a transition?

                Choose an answer to see the explanation.

              6. Question 6 of 8What does a cross-document view transition need?

                Choose an answer to see the explanation.

              7. Question 7 of 8Asha taps the middle of the screen while the cards are still gliding. What does her tap reach?

                Choose an answer to see the explanation.

              8. Question 8 of 8For a visitor who asks for less motion, which way gets the new order on screen sooner?

                Choose an answer to see the explanation.

              Key takeaways

              • A view transition takes snapshots, pauses rendering while the update callback runs, then animates its pseudo-elements.
              • While rendering is paused, nothing moves, not even compositor animations. Do slow work before startViewTransition().
              • Event Timing ends at the snapshot's frame, so INP can miss the wait. Measure when the new state appears.
              • Chrome gives an update callback 4 seconds. Then it skips the animation, and ready fails with a TimeoutError.
              • Every view-transition-name adds a snapshot, a group and five animations. Name only what must move, only while it moves.
              • A cross-document transition needs @view-transition on both pages, and delays the next page's first paint.
              • Taps reach html while the animation plays. Keep it short, and skip it for prefers-reduced-motion: reduce.

              Remember the one-liner.
              A view transition freezes the page, photographs it and animates: keep each step short, and skip it for anyone who asks for less motion.

              Sources

              The facts on this page come from these specs, docs and source files:

              Coming next: Measuring smoothness. You'll count dropped and partly presented frames, like the ones in this lesson's traces.

              CompleteFrontend Clear concepts. Working examples.