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

瀏覽器相容性

另見