简体   繁体   中英

How to add class to nav bar if scroll is on top with JavaScript

I want to add a class named "is-attached" to the navigation bar if the scroll is on the top of the page.

It's something quite easy to do with jQuery, but I'm trying to do it with vanilla js, how could I do that?

You can check for scrollTop property to see if page is scrolled or not, and if not add the class. try this document.body.scrollTop

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