简体   繁体   English

如果滚动条位于JavaScript顶部,如何将类添加到导航栏

[英]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. 如果滚动条位于页面顶部,我想向导航栏添加一个名为“ is-attached”的类。

It's something quite easy to do with jQuery, but I'm trying to do it with vanilla js, how could I do that? 使用jQuery相当容易,但是我正在尝试使用香草js,我该怎么做?

You can check for scrollTop property to see if page is scrolled or not, and if not add the class. 您可以检查scrollTop属性以查看页面是否滚动,如果没有滚动,则添加类。 try this document.body.scrollTop 试试这个document.body.scrollTop

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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