[英]I don't know what i am doing wrong, I want to change the background color of the navbar when the page gets scrolled
This is what i am doing, I used bootstrap to style this nav.这就是我正在做的,我使用引导程序来设计这个导航。 The nav basically collapses and a toggle button appears.
导航基本上折叠并出现一个切换按钮。
<nav id="mainNavbar" class="navbar navbar-expand-lg navbar-dark py-0 fixed-top ">
<div class="container-fluid">
<a class="navbar-brand" href="#">CANDY</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarTogglerDemo01"
aria-controls="navbarTogglerDemo01" aria-expanded="false" aria-label="Toggle navigation"
class="btn btn-info">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarTogglerDemo01">
<ul class="navbar-nav ">
<li class="nav-item">
<a class="nav-link" href="#">HOME</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">ABOUT</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">TICKETS</a>
</li>
</ul>
</div>
</nav>
this is the css for styling it when scrolled.这是滚动时设置样式的 css。 Nothing else was used to style it
没有其他东西用来设计它
#mainNavbar.scrolled{
background-color: #ac3bac;
transition: background 500ms;
}
But the problem is that it isn't toggling the class 'scrolled' to style it.但问题是它并没有切换“滚动”类来设置它的样式。 I have tried multiple approaches but none of them are working.
我尝试了多种方法,但都没有奏效。 If possible pls help me complete it
如果可能,请帮我完成它
<script>enter code here
$(function () {
$(document).scroll(function () {
var $nav = $("#mainNavbar");
$nav.toggleClass("scrolled", $(this).scrollTop() > $nav.height());
});
});
</script>
I just tried your code here , which i hope it will help you to focus on your problem.我刚刚在这里尝试了您的代码,希望它能帮助您专注于您的问题。
I have add some lorem in order to make the window scroll properly, in order to trigger the event.我添加了一些 lorem 以使窗口正确滚动,以触发事件。
<div>lorem...</div>
Everything should work fine, are you sure have you add all the proper scripts?一切正常,您确定添加了所有正确的脚本吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.