[英]I have the scroll behavior set to smooth in CSS but how to write it in JS?
我已經使用 CSS 將滾動功能設置為平滑
scroll-behavior: smooth
我被告知可以使用 scrollIntoView 或 scrollTo 來完成,但我對此有點初學者。 我如何將其轉換為純 JS?
scroll-behavior CSS 屬性使元素內的內容平滑滾動。 而Element.scrollIntoView用於以編程方式滾動到元素。
例如,如果您想使用scroll-behavior: smooth
滾動到頁面的標題,您可以選擇behavior
選項並將其設置為smooth
。
var header = document.getElementById("header")
header.scrollIntoView({ behavior: "smooth" })
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.