NavigationActivation

可用性有限

此特性不是基线特性,因为它在一些最广泛使用的浏览器中不起作用。

实验性: 这是一项实验性技术
在生产中使用此技术之前,请仔细检查浏览器兼容性表格

NavigationActivation 接口是 Navigation API 的一部分,用于表示最近的跨文档导航。它包含导航类型以及出站和入站文档的历史记录条目。

通过 PageSwapEvent.activationNavigation.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

浏览器兼容性

另见