I am new to the Bootstrap framework and I would like to change the color of my navbar when scrolling on the different sections. When the background color of the section is white, the background color of the navbar should change to a red color, but my javascript isn' t working...
Could you please help me? I was unable to find a solution to my problem. Here's my codepen
$(document).ready(function() {
var scroll_start = 0;
var startchange = $('#part1');
var offset = startchange.offset();
$(document).scroll(function() {
scroll_start = $(this).scrollTop();
if (scroll_start > offset.top) {
$('.navbar-fixed-left').css('background-color', '#f9476c !important;');
} else {
$('.navbar-fixed-left').css('background-color', 'rgba(255,255,255,0.6)');
}
});
});
#part1 {
width: 100%;
height: 100vh;
background-color: white;
}
#part2 {
width: 100%;
height: 100vh;
background-color: #e9b8ac;
}
#part3 {
width: 100%;
height: 100vh;
background-color: white;
}
#part4 {
width: 100%;
height: 100vh;
background-color: #e9b8ac;
}
#spy {
position: fixed;
z-index: 5;
height: 100vh;
}
.op {
background-color: rgba(255, 255, 255, 0.6);
}
.op :hover {
background-color: rgba(255, 255, 255, 1);
}
.navbar-fixed-left .nav-link {
color: black;
text-decoration: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>
<body>
<div class="d-flex align-items-xl-center" id="spy">
<nav class="navbar navbar-fixed-left">
<ul class="navbar-nav">
<li class="nav-item op"><a class="nav-link" href="#part1">Home</a></li>
<li class="nav-item op"><a class="nav-link" href="#part2">About</a> </li>
<li class="nav-item op"><a class="nav-link" href="#part3">Price</a> </li>
<li class="nav-item op"><a class="nav-link" href="#part4">Contact</a> </li>
</ul>
</nav>
</div>
<section id="part1">
</section>
<section id="part2">
</section>
<section id="part3">
</section>
<section id="part4">
</section>
</body>
</html>