Window.scrollBy()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2020.
Window.scrollBy()
方法在窗口中按指定的偏移量滚动文档。
语法
js
scrollBy(x-coord, y-coord)
scrollBy(options)
参数
返回值
无(undefined
)。
示例
向下滚动一页:
js
window.scrollBy(0, window.innerHeight);
向上滚动一页:
js
window.scrollBy(0, -window.innerHeight);
使用 options
:
js
window.scrollBy({
top: 100,
left: 100,
behavior: "smooth",
});
备注
window.scrollBy()
滚动指定的距离,而 window.scroll()
滚动至文档中的绝对位置。参见 window.scrollByLines()
和 window.scrollByPages()
。
规范
Specification |
---|
CSSOM View Module # dom-window-scrollby |
浏览器兼容性
Report problems with this compatibility data on GitHubdesktop | mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
scrollBy | ||||||||||||
options.behavior parameter | ||||||||||||
options.left parameter | ||||||||||||
options.top parameter |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
- See implementation notes.
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.