The pageswap
event is fired when you navigate across documents, when the previous document is about to unload.
This is useful in the case of cross-document (MPA) view transitions for manipulating an active transition from the outbound page of a navigation. For example, you might wish to skip the transition, or customize the outbound transition animation via JavaScript.
It also provides access to the navigation type and current and destination document history entries.
Use the event name in methods like addEventListener()
, or set an event handler property.
addEventListener("pageswap", (event) => {});
onpageswap = (event) => {};
window.addEventListener("pageswap", async (e) => {
if (e.viewTransition) {
const currentUrl = e.activation.from?.url
? new URL(e.activation.from.url)
: null;
const targetUrl = new URL(e.activation.entry.url);
if (isProfilePage(currentUrl) && isHomePage(targetUrl)) {
document.querySelector(`#detail main h1`).style.viewTransitionName =
"name";
document.querySelector(`#detail main img`).style.viewTransitionName =
"avatar";
await e.viewTransition.finished;
document.querySelector(`#detail main h1`).style.viewTransitionName =
"none";
document.querySelector(`#detail main img`).style.viewTransitionName =
"none";
}
if (isProfilePage(targetUrl)) {
const profile = extractProfileNameFromUrl(targetUrl);
document.querySelector(`#${profile} span`).style.viewTransitionName =
"name";
document.querySelector(`#${profile} img`).style.viewTransitionName =
"avatar";
await e.viewTransition.finished;
document.querySelector(`#${profile} span`).style.viewTransitionName =
"none";
document.querySelector(`#${profile} img`).style.viewTransitionName =
"none";
}
}
});