I created this pen at CodePen.io , the page scrolls smoothly to different sections on clicking any item in the nav (navigation)
Here is the jQuery code.
// Smooth Scroll on clicking navigation items
$('nav a').click(function() {
var $href = $(this).attr('href');
$('body').stop().animate({scrollTop: $($href).offset().top}, 1000);
// add class "active" to nav items on click
$('nav a').removeClass('active');
$(this).addClass('active');
return false;
});
I have already added the class "active" when someone clicks on any of the links inside the nav, but how to add class active when someone scrolls to that section using the scroll bar not by clicking on the link ?
Look at the pen here
Your suggestions will be appreciated, Thank you in advance !