Element.scrollBy()

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

Syntax

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

Parameters

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

- or -

例子

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

使用 options:

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

规范

规范 状态 注释
CSS Object Model (CSSOM) View Module
element.scrollBy()
Working Draft Initial definition.

 浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
scrollByChrome Full support 45Edge No support NoFirefox Full support YesIE No support NoOpera Full support 32Safari Full support 10WebView Android Full support 45Chrome Android Full support 45Firefox Android Full support YesOpera Android Full support 32Safari iOS No support NoSamsung Internet Android Full support Yes
ScrollToOptions parameterChrome Full support 45Edge No support NoFirefox Full support YesIE No support NoOpera Full support 32Safari No support NoWebView Android Full support 45Chrome Android Full support 45Firefox Android Full support YesOpera Android Full support 32Safari iOS No support NoSamsung Internet Android Full support Yes

Legend

Full support  
Full support
No support  
No support