HTML:标记语言
CSS:样式语言
JavaScript:脚本语言
Web API:编程接口
所有 Web 技术
学习 Web 开发
发现我们的工具
更好地了解 MDN
此特性已相当成熟,可在许多设备和浏览器版本上使用。自 2020 年 1 月 起,所有主流浏览器均已支持。
Element 接口的 scroll() 方法可以将元素滚动到指定元素内的一组特定坐标。
Element
scroll()
scroll(xCoord, yCoord) scroll(options)
xCoord
希望显示在左上角的元素水平轴的像素值。
yCoord
希望显示在左上角的元素垂直轴的像素值。
options
包含以下属性的对象:
top
指定要滚动窗口或元素的 Y 轴上的像素数。
left
指定要滚动窗口或元素的 X 轴上的像素数。
behavior
确定滚动是即时发生的还是平滑动画的。此选项是一个字符串,必须是以下值之一
smooth
instant
auto
scroll-behavior
无(undefined)。
undefined
// Put the 1000th vertical pixel at the top of the element element.scroll(0, 1000);
使用 options
element.scroll({ top: 100, left: 100, behavior: "smooth", });
加载中…