Window:scroll() 方法
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.scroll()
方法滚动窗口至文档中的特定位置。
语法
js
scroll(xCoord, yCoord)
scroll(options)
参数
返回值
无(undefined
)。
示例
html
<!-- 把纵轴上第 100 个像素置于窗口顶部 -->
<button onclick="scroll(0, 100);">点击以向下滚动到第 100 个像素</button>
使用 options
:
js
window.scroll({
top: 100,
left: 100,
behavior: "smooth",
});
备注
Window.scrollTo()
实际上和该方法是相同的。有关相对滚动,请参见 Window.scrollBy()
、Window.scrollByLines()
和 Window.scrollByPages()
。
有关滚动元素,请参见 Element.scrollTop
和 Element.scrollLeft
。
规范
Specification |
---|
CSSOM View Module # dom-window-scroll |
浏览器兼容性
Report problems with this compatibility data on GitHubdesktop | mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
scroll | ||||||||||||
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.