歷史:scrollRestoration 屬性

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2020 年 1 月⁩ 起,所有主流瀏覽器均已支援。

History 介面的 scrollRestoration 屬性允許 Web 應用程式在歷史導航時顯式設定預設的滾動恢復行為。

以下之一:

auto

使用者滾動到的頁面位置將被恢復。

manual

頁面位置不會被恢復。使用者必須手動滾動到該位置。

示例

查詢當前的滾動恢復行為

js
const scrollRestoration = history.scrollRestoration;
if (scrollRestoration === "manual") {
  console.log(
    "The location on the page is not restored, user will need to scroll manually.",
  );
}

防止自動頁面位置恢復

js
history.scrollRestoration = "manual";

規範

規範
HTML
# dom-history-scroll-restoration-dev

瀏覽器相容性