I have done a custom menu with a parallax style. The menu is fixed on top and I want to change the active class for the menu when page scrolls. I have done it by click function so by clicking the menu the active class change to that particular menu. I created this menu with id not 'href="#home"'. Most of the code I find is done by href="#" and nav. I don't want the menu to be one with nav navbar. So can it be done by id?
My codes are below
$("#home-btn").on('click',function() {
$('html, body').animate({
scrollTop : $("#home").offset().top - 10
}, 1000);
$('.main-menu ul li').removeClass('active');
$('#home-btn').parent().addClass('active');
});
$("#service-btn").on('click',function() {
$('html, body').animate({
scrollTop : $("#service").offset().top - 45
}, 1000);
$('.main-menu ul li').removeClass('active');
$('#service-btn').parent().addClass('active');
});
$("#about-btn").on('click',function() {
$('html, body').animate({
scrollTop : $("#about").offset().top - 120
}, 1000);
$('.main-menu ul li').removeClass('active');
$('#about-btn').parent().addClass('active');
});
$("#contact-btn").on('click',function() {
$('html, body').animate({
scrollTop : $("#contact").offset().top - 120
}, 2000);
$('.main-menu ul li').removeClass('active');
$('#contact-btn').parent().addClass('active');
});
.top-adj {
margin-top: 130px;
}
.main-menu {
position: fixed;
top: 0;
left: 0;
right: 0;
background: #f1f1f1;
}
.main-menu ul {
display: flex;
align-items: center;
justify-content: space-between;
}
.main-menu ul li {
display: block;
font-weight: 600;
margin-left: 25px;
}
.main-menu ul li:first-child {
margin-left: 0px;
}
.main-menu ul li a {
color: #000;
display: block;
cursor: pointer;
padding: 25px 10px;
position: relative;
border-bottom: 4px solid;
border-color: transparent;
}
.main-menu ul li a:hover {
text-decoration: none;
}
.main-menu ul li.active a {
padding: 25px 10px;
border-bottom: 4px solid #104377;
}
.section {
width: 100%;
height: 100vh;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="main-menu">
<ul>
<li class="active"><a id="home-btn">Home</a></li>
<li><a id="service-btn">Services</a></li>
<li><a id="about-btn">About</a></li>
<li><a id="contact-btn">Contact</a></li>
</ul>
</div>
<div class="top-adj"></div>
<div class="section" id="home">Home</div>
<div class="section" id="service">Service</div>
<div class="section" id="about">About</div>
<div class="section" id="contact">Contact</div>