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", });
載入中…