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 |
浏览器兼容性
加载中…