--- title: Element.scrollBy() slug: Web/API/Element/scrollBy tags: - API - Element - Method - Reference - scrollBy translation_of: Web/API/Element/scrollBy ---
scrollBy() 方法是使得元素滚动一段特定距离的 {{domxref("Element")}} 接口。
element.scrollBy(x-coord, y-coord); element.scrollBy(options)
x-coord 是元素要在横轴上滚动的距离。y-coord 是元素要在纵轴上滚动的距离。- or -
options 是一个 {{domxref("ScrollToOptions")}} 字典。// 让元素滚动 element.scrollBy(300, 300);
使用 options:
element.scrollBy({
top: 100,
left: 100,
behavior: 'smooth'
});
| 规范 | 状态 | 注释 |
|---|---|---|
| {{ SpecName('CSSOM View', '#dom-element-scrollby-options-options', 'element.scrollBy()') }} | {{ Spec2('CSSOM View') }} | Initial definition. |
{{Compat("api.Element.scrollBy")}}