The startViewTransition()
method of the View Transitions API starts a new view transition and returns a ViewTransition
object to represent it.
When startViewTransition()
is invoked, a sequence of steps is followed as explained in The view transition process.
startViewTransition(callback)
A ViewTransition
object instance.
In our Basic View Transitions demo, the updateView()
function handles both browsers that do and don't support the View Transitions API. In supporting browsers, we invoke startViewTransition()
to set off the view transition process without worrying about the return value.
function updateView(event) {
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;
};
if (!document.startViewTransition) {
displayNewImage();
return;
}
const transition = document.startViewTransition(() => displayNewImage());
}