I am working on a web page in Bootstrap 4, that has sections with light and dark backgrounds and a fixed navbar. The navbar is dark (has the css class bg-dark
) and, while it is easily visible against the light sections, it is indistinguishable against the dark ones.
I have added Bootstrap's scroll-spy to the page, but what it does is add the active
class to the navbar items, as seen below:
.page-section {
padding: 70px 10px
}
.page-section.bg-dark * {
color: #fff;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<body data-spy="scroll" data-target=".navbar" data-offset="15">
<nav class="navbar navbar-expand-sm navbar-dark bg-dark fixed-top">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#section1">Section 1</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#section2">Section 2</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#section3">Section 3</a>
</li>
</ul>
</nav>
<div id="section1" class="container-fluid bg-light page-section">
<h1>Section 1</h1>
<p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
<p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
</div>
<div id="section2" class="container-fluid bg-dark page-section">
<h1>Section 2</h1>
<p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
<p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
</div>
<div id="section3" class="container-fluid bg-light page-section">
<h1>Section 3</h1>
<p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
<p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
</div>
</body>
How can I change the navbar's navbar-dark bg-dark
to navbar-light bg-light
when the user reaches div with id section3
or (even better) with class bg-light
?
There's no built-in way to change the target of the active class, so you're left with a javascript option. Fortunately, Bootstrap 4 does provide an event that gets triggered when a new active item is set. One odd quark with it is that the documentation suggests you can add the listener event to the element with
data-spy
. This does not work. You have to add it to thewindow
object (https://stackoverflow.com/a/48694139/854246). Here's a code snippet:I would make this a stack snippet, but Stack Overflow doesn't like it when you listen to events on theApparently it does support it. It just doesn't like when you usewindow
object for security reasons (sandboxed iframe and whatnot), so here is a fiddle demonstrating this solution:console.log
https://jsfiddle.net/jmarikle/jzg7ok0v/
Use js code for toggle class
here a test with you need, but for scroll for 1 px and after begining the change for some states. sorry my bad english.
https://codepen.io/jhonycertz/pen/eQPbXy