I am new to javascript I am trying to make the menu to be highlighted when the user scrolls to the bookmarked page. and if possible please code for smooth scrolling for the bookmarked page.
My HTML :
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="nav navbar-nav ml-auto aw-menu">
<li class="nav-item item"><a class="nav-link" href="#index.html">Home</a></li>
<li class="nav-item item"><a class="nav-link" href="#">Updates</a></li>
<li class="nav-item item"><a class="nav-link" href="#about">About us</a></li>
<li class="nav-item item"><a class="nav-link" href="#course">Courses</a></li>
<li class="nav-item item"><a class="nav-link" href="#">Cells</a></li>
<li class="nav-item item"><a class="nav-link" href="#">Library</a></li>
<li class="nav-item item"><a class="nav-link" href="#">Placements</a></li>
<li class="nav-item item"><a class="nav-link" href="#">Contact-us</a></li>
</ul>
</div>
My CSS:
.active{
background-color: #64b3f4;
font-weight: 300;
color: white;
border: 2px;
}
My javascript :
$(window).on("scroll", function() {
var currentPos = $(window).scrollTop();
$('.nav li a').each(function() {
var sectionLink = $(this);
// capture the height of the navbar
var navHeight = $('.aw-menu').outerHeight() + 1;
var section = $(sectionLink.attr('href'));
// subtract the navbar height from the top of the section
if(section.position().top - navHeight <= currentPos && sectionLink.offset().top + section.height()> currentPos) {
$('.nav li').removeClass('active');
sectionLink.parent().addClass('active');
} else {
sectionLink.parent().removeClass('active');
}
});
});