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

View in English Always switch to English

Element.scrollBy()

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月⁩.

scrollBy() 方法是使得元素滚动一段特定距离的 Element 接口。

Syntax

element.scrollBy(x-coord, y-coord);
element.scrollBy(options)

Parameters

  • x-coord 是元素要在横轴上滚动的距离。
  • y-coord 是元素要在纵轴上滚动的距离。

- or -

例子

js
// 让元素滚动
element.scrollBy(300, 300);

使用 options:

js
element.scrollBy({
  top: 100,
  left: 100,
  behavior: "smooth",
});

规范

Specification
CSSOM View Module
# dom-element-scrollby

浏览器兼容性