W3cubDocs

/Web APIs

ViewTransition: finished property

Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

The finished read-only property of the ViewTransition interface is a Promise that fulfills once the transition animation is finished, and the new page view is visible and interactive to the user.

finished only rejects if the callback passed to document.startViewTransition()throws or returns a promise that rejects, which indicates that the new state of the page wasn't created.

If a transition animation fails to start, or is skipped during the animation using ViewTransition.skipTransition(), the end state is still reached therefore finished still fulfills.

Value

A promise.

Examples

Different transitions for different navigations

Sometimes certain navigations will require specifically tailored transitions, for example a 'back' navigation may want a different transition to a 'forward' navigation. The best way to handle such cases is to set a class name on the <html> element, handle the transition — applying the correct animation using a tailored selector — and then remove the class name once the transition is finished.

js

async function handleTransition() {
  if (isBackNavigation) {
    document.documentElement.classList.add("back-transition");
  }

  const transition = document.startViewTransition(() =>
    updateTheDOMSomehow(data),
  );

  try {
    await transition.finished;
  } finally {
    document.documentElement.classList.remove("back-transition");
  }
}

Note: isBackNavigation isn't a built-in feature; it's a theoretical function that could perhaps be implemented using the Navigation API or similar.

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
finished 111 111 No No 97 No 111 111 No No No 22.0

See also

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/ViewTransition/finished