document.startViewTransition({
update() {
displayedImage.src = `${baseURL}${images[newId].filename}`;
displayedImage.alt = images[newId].alt;
displayedImage.setAttribute("data-id", newId);
caption.textContent = images[newId].alt;
},
types: ["backwards"],
});Using view transition types / notebook
Using view transition types
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
html:active-view-transition {
:root {
view-transition-name: none;
}
.displayed-img {
view-transition-name: image;
}
figcaption {
view-transition-name: caption;
}
&::view-transition-old(caption) {
animation-name: fade-out;
}
&::view-transition-new(caption) {
animation-name: fade-in;
animation-delay: 0.6s;
}
}html:active-view-transition-type(forwards) {
&::view-transition-old(image) {
animation-name: slide-out-to-left;
}
&::view-transition-new(image) {
animation-name: slide-in-from-right;
}
}
html:active-view-transition-type(backwards) {
&::view-transition-old(image) {
animation-name: slide-out-to-right;
}
&::view-transition-new(image) {
animation-name: slide-in-from-left;
}
}
html:active-view-transition-type(upwards) {
&::view-transition-old(image) {
animation-name: slide-out-to-top;
}
&::view-transition-new(image) {
animation-name: slide-in-from-top;
animation-delay: 0.6s;
}
}::view-transition-group(*) {
animation-duration: 0.6s;
}@view-transition {
navigation: auto;
types: slide;
}html:active-view-transition-type(slide) {
:root {
view-transition-name: none;
}
section {
view-transition-name: chapter;
}
&::view-transition-old(chapter) {
animation-name: slide-out-to-left;
}
&::view-transition-new(chapter) {
animation-name: slide-in-from-right;
}
}const determineTransitionType = (oldNavigationEntry, newNavigationEntry) => {
const currentURL = oldNavigationEntry.url;
const destinationURL = newNavigationEntry.url;
function determinePageIndex(url) {
const array = url.split("/");
const slug = array[array.length - 1];
if (slug.indexOf("html") === -1) {
return 0;
}
const pageIndex = slug.replace("index", "").replace(".html", "");
if (pageIndex === "") {
return 0;
}
return parseInt(pageIndex, 10);
}
const currentPageIndex = determinePageIndex(currentURL);
const destinationPageIndex = determinePageIndex(destinationURL);
if (currentPageIndex > destinationPageIndex) {
return "backwards";
} else if (currentPageIndex < destinationPageIndex) {
return "forwards";
}
};window.addEventListener("pageswap", async (e) => {
const transitionType = determineTransitionType(
e.activation.from,
e.activation.entry,
);
console.log(`pageSwap: ${transitionType}`);
e.viewTransition.types.add(transitionType);
});window.addEventListener("pagereveal", async (e) => {
const transitionType = determineTransitionType(
navigation.activation.from,
navigation.activation.entry,
);
console.log(`pageReveal: ${transitionType}`);
if (transitionType !== undefined) {
e.viewTransition.types.add(transitionType);
}
});html:active-view-transition {
nav {
view-transition-name: none;
}
section {
view-transition-name: chapter;
}
}
html:active-view-transition-type(forwards) {
&::view-transition-old(chapter) {
animation-name: slide-out-to-left;
}
&::view-transition-new(chapter) {
animation-name: slide-in-from-right;
}
}
html:active-view-transition-type(backwards) {
&::view-transition-old(chapter) {
animation-name: slide-out-to-right;
}
&::view-transition-new(chapter) {
animation-name: slide-in-from-left;
}
}@view-transition {
navigation: auto;
}