Hi.
I want to make a navigation bar that extends to the full width of the window and changes background color when scrolled to the next section and goes back to its default style when scrolled back up to home.
This is what it looks like now.
It still shows the black background when scrolled back to home and looks off. I want it to change immediately to its default style as soon as the user scrolled to home.
I’m having a wee bit of trouble in the jQuery (beginner here), I think in the if-else statement and the style since it’s position: fixed. There must be other ways to achieve this but I don’t have much idea on what those are.
If anyone can give me any clue on how to achieve what I’m trying to do, it would be a huge help. Thank you so much
Here are the codes.
.html
<div class="header-whole">
<div>
<img class="header-logo" src="img.png"/>
<img class="header-logo" id="logo-right" src="img.png"/>
</div>
<div class="main-nav">
<ul class="main-nav-menu">
<li><a href="#about-us">ABOUT US</a></li>
<li><a href="#facility">FACILITY</a></li>
<li><a href="#services">SERVICES</a></li>
<li><a href="#get-started">GET STARTED</a></li>
</ul>
</div>
<div class="spinner-master2">
<input type="checkbox" id="spinner-form2" />
<label for="spinner-form2" class="spinner-spin2">
<div class="spinner2 diagonal part-1"></div>
<div class="spinner2 horizontal"></div>
<div class="spinner2 diagonal part-2"></div>
</label>
</div>
</div>
.css
.header-logo {
padding: 0 !important;
}
.header-whole {
width: 90%;
position: fixed;
left: 50%;
transform: translate(-50%, 0);
display: flex;
justify-content: space-between;
max-width: 100% !important;
padding: 20px;
}
.main-nav{
display: block;
}
.main-nav ul {
display: flex;
text-decoration: none !important;
margin: 0 !important;
padding-top: 7px;
}
.main-nav li {
padding: 0 20px;
list-style: none;
}
.main-nav a {
text-decoration: none;
color: #ffffff;
}
.navbar-color {
background-color: #000000 !important;
width: 100% !important;
}
.js
jQuery(function($){
var $navbar = $('.header-whole');
$(window).scroll(function(event) {
var $current = $(this).scrollTop();
if( $current > 0 ){
$navbar.addClass('navbar-color');
} else {
$navbar.removeClass('navbar-color');
}
});
});