使用引导框架,我试图将导航栏的背景颜色从透明更改为白色。当我使用navbar-shrink类稍微滚动时,它当前正在工作。但是,我只想在导航栏缩小时不立刻进入第一部分标签,就将导航栏的颜色从透明更改为白色...
#mainNav.navbar-shrink {
border-bottom: 1px solid rgba(33,37,41,.1);
background-color: #fff;
}
.container {
background-color: grey;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<nav class="navbar navbar-expand-lg navbar-light fixed-top" id="mainNav">
<div class="container">
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarResponsive">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link js-scroll-trigger selected" href="#about">About</a>
</li>
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="#services">Services</a>
</li>
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="#portfolio">Projects</a>
</li>
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="#contact">Contact</a>
</li>
</ul>
</div>
</div>
</nav>
<section class="bg-primary first" id="about">
<div class="container">
<div class="row">
</section>
我希望仅当我滚动到section标签(id = about)时才改变背景颜色,而不是在导航栏缩小时才改变
答案 0 :(得分:2)
如果您使用ScrollSpy插件来处理活动导航链接的更新,则可以使用以下解决方案。当#about
部分滚动到时,导航栏只会变成白色。
$('[data-spy="scroll"]').on('activate.bs.scrollspy', function (e) {
var tabId = $(e.target).find('a').attr("href");
if(tabId == "#about"){
$("#mainNav").css("background-color","#fff")
}
})
答案 1 :(得分:0)
$(document).ready(function () {
$(window).on("scroll", function () {
if ($(window).scrollTop() >= 800) {
$(".navbar").addClass("navbar-shrink ");
} else {
$(".navbar").removeClass("navbar-shrink ");
}
});
});