Přejít k hlavnímu obsahu
← ⚡ JavaScript & the browser·Modul C9 · Lekce 9
ÚkolWrite transition(fn) that calls document.startViewTransition(fn) if it exists, else just fn(). Wrap a DOM update that sets <p id="out">: starter has 'before', after call should say 'after'. Log document.getElementById('out').textContent. Also log typeof document.startViewTransition.

View Transitions for crossfade

125 XP9 min
Teorie

Native page-state transitions

document.startViewTransition(callback) animates between DOM states for free. The browser snapshots the current DOM, runs your callback (which mutates the DOM), snapshots the new DOM, and crossfades between them.

document.startViewTransition(() => {
  document.body.innerHTML = "<h1>New state</h1>";
});

What you don't have to write

  • Capture before-state.
  • Animate via CSS keyframes.
  • Capture after-state.
  • Diff and patch DOM.

The browser does all of it. Default animation: a soft crossfade.

Browser support note

Chrome and Edge have it (since 2023). Safari shipped support in 2024. Firefox is behind a flag at the time of writing. Always check if (document.startViewTransition) before calling.

Graceful degradation

function transition(update) {
  if (document.startViewTransition) {
    document.startViewTransition(update);
  } else {
    update();
  }
}

Browsers without support still get the new state — they just skip the animation.

🔒

Zaregistruj se a začni programovat

Teorie je otevřená pro každého. Editor, živý náhled a check se odemknou s 7denní zkušební verzí zdarma — karta vyžadována, zrušíš kdykoli.

Registrace — zkušební verze zdarma →

Prvních 10 lekcí v každé trase je zdarma. Karta není potřeba.

← PředchozíDalší lekce →

Jeden tip k Pythonu nebo webu denně — e-mailem

Krátké, ručně psané, žádný spam. Odhlásit jedním klikem.