HTML:标记语言
CSS:样式语言
JavaScript:脚本语言
Web API:编程接口
所有 Web 技术
学习 Web 开发
发现我们的工具
更好地了解 MDN
此特性已相当成熟,可在许多设备和浏览器版本上使用。自 2020 年 1 月 起,所有主流浏览器均已支持。
Element 接口的 scrollBy() 方法可按给定量滚动元素。
Element
scrollBy()
scrollBy(xCoord, yCoord) scrollBy(options)
xCoord
您希望水平滚动的像素值。
yCoord
您希望垂直滚动的像素值。
options
包含以下属性的对象:
top
指定要滚动窗口或元素的 Y 轴上的像素数。
left
指定要滚动窗口或元素的 X 轴上的像素数。
behavior
指定滚动应该是平滑动画(smooth)、一次性瞬间跳转(instant)还是让浏览器自行选择(auto,默认值)。
smooth
instant
auto
无(undefined)。
undefined
// scroll an element element.scrollBy(300, 300);
使用 options
element.scrollBy({ top: 100, left: 100, behavior: "smooth", });
加载中…