繁体   English   中英

我在 CSS 中将滚动行为设置为平滑,但如何在 JS 中编写它?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM