简体   繁体   中英

I have the scroll behavior set to smooth in CSS but how to write it in JS?

I've set the scroll functionality to smooth using CSS

scroll-behavior: smooth

I've been told it can be done using scrollIntoView or scrollTo, but I am a bit beginner to this. How do I turn that to pure JS?

scroll-behavior CSS property make the contents within an element scroll smoothly. While Element.scrollIntoView is used to programmatically scroll to an element.

If you want for example scroll to the header of the page with scroll-behavior: smooth you can opt-in the behavior option and set it to smooth .

var header = document.getElementById("header")
header.scrollIntoView({ behavior: "smooth" })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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