此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

History:scrollRestoration 属性

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2020年1月⁩.

History 接口的 scrollRestoration 属性允许 web 应用显式设置在历史导航(如前进或后退)时的默认滚动位置恢复行为。

以下两种取值之一:

auto

会恢复用户在页面上的滚动位置。

manual

不会恢复页面的滚动位置。用户需要手动滚动到原来的位置。

示例

查询当前的滚动恢复行为

js
const scrollRestoration = history.scrollRestoration;
if (scrollRestoration === "manual") {
  console.log("页面位置不会自动恢复,用户需要手动滚动。");
}

禁止自动恢复页面位置

js
history.scrollRestoration = "manual";

规范

Specification
HTML
# dom-history-scroll-restoration-dev

浏览器兼容性