NavigationActivation
NavigationActivation 介面是 Navigation API 的一部分,用於表示最近的跨文件導航。它包含導航型別以及出站和入站文件的歷史記錄條目。
透過 PageSwapEvent.activation 和 Navigation.activation 屬性訪問此物件。請注意,在每種情況下,NavigationActivation 都代表不同的導航。
Navigation.activation代表有關當前頁面的導航資訊。PageSwapEvent.activation代表有關下一個頁面的導航資訊。
例項屬性
entry只讀 實驗性-
包含一個
NavigationHistoryEntry物件,表示導航中入站(“到”)文件的歷史記錄條目。這等同於入站文件被啟用時的Navigation.currentEntry屬性。 from只讀 實驗性-
包含一個
NavigationHistoryEntry物件,表示導航中出站(“從”)文件的歷史記錄條目。 -
包含一個字串,指示導航的型別。
示例
js
window.addEventListener("pagereveal", async (e) => {
// If the "from" history entry does not exist, return
if (!navigation.activation.from) return;
// Only run this if an active view transition exists
if (e.viewTransition) {
const fromUrl = new URL(navigation.activation.from.url);
const currentUrl = new URL(navigation.activation.entry.url);
// Went from profile page to homepage
// ~> Set VT names on the relevant list item
if (isProfilePage(fromUrl) && isHomePage(currentUrl)) {
const profile = extractProfileNameFromUrl(fromUrl);
// Set view-transition-name values on the elements to animate
document.querySelector(`#${profile} span`).style.viewTransitionName =
"name";
document.querySelector(`#${profile} img`).style.viewTransitionName =
"avatar";
// Remove names after snapshots have been taken
// so that we're ready for the next navigation
await e.viewTransition.ready;
document.querySelector(`#${profile} span`).style.viewTransitionName =
"none";
document.querySelector(`#${profile} img`).style.viewTransitionName =
"none";
}
// Went to profile page
// ~> Set VT names on the main title and image
if (isProfilePage(currentUrl)) {
// Set view-transition-name values on the elements to animate
document.querySelector(`#detail main h1`).style.viewTransitionName =
"name";
document.querySelector(`#detail main img`).style.viewTransitionName =
"avatar";
// Remove names after snapshots have been taken
// so that we're ready for the next navigation
await e.viewTransition.ready;
document.querySelector(`#detail main h1`).style.viewTransitionName =
"none";
document.querySelector(`#detail main img`).style.viewTransitionName =
"none";
}
}
});
注意:請參閱Chrome DevRel 團隊成員列表以獲取此程式碼所引用的即時演示。
規範
| 規範 |
|---|
| HTML # navigationactivation |
瀏覽器相容性
載入中…