docs.rodeo

MDN Web Docs mirror

Document: startViewTransition() method

{{APIRef("View Transition API")}} 

The startViewTransition() method of the {{domxref("Document")}}  interface starts a new same-document (SPA) view transition and returns a {{domxref("ViewTransition")}}  object to represent it.

When startViewTransition() is invoked, a sequence of steps is followed as explained in The view transition process.

Syntax

startViewTransition()
startViewTransition(updateCallback)

Parameters

Return value

A {{domxref("ViewTransition")}}  object instance.

Examples

Basic usage

In our Basic SPA View Transitions demo, the updateView() function handles both browsers that do and don’t support the View Transition API. In supporting browsers, we invoke startViewTransition() to trigger the view transition process without worrying about the return value.

function updateView(event) {
  // Handle the difference in whether the event is fired on the <a> or the <img>
  let targetIdentifier;
  if (event.target.firstChild === null) {
    targetIdentifier = event.target;
  } else {
    targetIdentifier = event.target.firstChild;
  }

  const displayNewImage = () => {
    const mainSrc = `${targetIdentifier.src.split("_th.jpg")[0]}.jpg`;
    galleryImg.src = mainSrc;
    galleryCaption.textContent = targetIdentifier.alt;
  };

  // Fallback for browsers that don't support View Transitions:
  if (!document.startViewTransition) {
    displayNewImage();
    return;
  }

  // With View Transitions:
  const transition = document.startViewTransition(() => displayNewImage());
}

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

See also

In this article

View on MDN