历史: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

浏览器兼容性