滚动经过锚点时,如何使导航栏更改 CSS 类?

我正在使用 Bootstrap 3,并希望在用户滚动经过我的大标题图像时实现 这种效果页.我需要导航栏的背景从透明变为白色.我查看了他们的代码,我知道它是用 javascript 完成的,甚至看到了 WHERE 我认为它发生了(在那个 JS 中查找 ID '#main-header')...

I'm using Bootstrap 3 and want to achieve this effect when the user scrolls past the large header image on my page. I need the background of the navbar to go from transparent to white. I looked in their code and I KNOW it is done with javascript, and even saw WHERE it was happening I think (look for the ID '#main-header' in that JS)...

除了不了解高级 Javascript 之外,我正在寻找一种在滚动超过某个点时将其应用于导航栏的方法.我的代码的类称为导航栏",我希望它在通过#main"时变成白色.如果您需要更多信息,请告诉我,如果有人想提供帮助,请提前致谢!

Not knowing advanced Javascript aside, I'm looking for a way to apply this to my navigation bar when scrolling past a certain point. The class for my code is called 'navbar' and I would like it to turn white when it passes "#main". Let me know if you need more information, and thanks in advance if anyone wants to help!

推荐答案

如果你使用 Twitter Bootstrap,这可以通过'Affix' 插件

If you're using Twitter Bootstrap this can be achieved with the 'Affix' plugin

设置起来非常简单,这里是文档

相关文章