"CodePen Home jQuery - Add Class to Navigation on Scroll"
Bootstrap 4.1.1 Snippet by ranjit1602

<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"> <script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <!------ Include the above in your HEAD tag ----------> <div class="container"> <div class="slide" id="one"> <h2>Container 1</h2> </div> <div class="slide" id="two"> <h2>Container 2</h2> </div> <div class="slide" id="three"> <h2>Container 3</h2> </div> </div> <div id="nav"> <ul> <li id="nav1" class="active"><a href="#one">container 1</a></li> <li id="nav2"><a href="#two">container 2</a></li> <li id="nav3"><a href="#three">container 3</a></li> </ul> </div>
* { font-family: Arial, sans-serif; } .slide { width: 100%; height: 100vh; display: flex; align-content: center; align-items: center; justify-content: center; color: #fff; } .slide#one { background: #525252; } .slide#two { background: #414141; } .slide#three { background: #313131; } #nav { position: fixed; top: 0; left: 0; background: #EC625F; height: 60px; width: 100%; display: flex; justify-content: space-around; align-items: center; box-shadow: 1px 1px 10px #393939; } #nav ul { padding: 0; margin: 0; height: 100%; width: 100%; display: flex; justify-content: space-around; align-items: center; } #nav ul li { display: flex; justify-content: center; align-items: center; list-style: none; flex-grow: 1; text-align: center; height: 100%; } #nav ul li.active { background-color: #e94c48; box-shadow: inset 0 0 1px #2d0605; } #nav ul li.active a { color: #fff; } #nav ul li a { color: #f7bbba; text-decoration: none; }
$(document).ready(function() { /*------------------------------------------------------ adds active class to the nav element where the scroll position is currently at ------------------------------------------------------*/ $(window).scroll(function() { //get current sroll position var scrollPosition = $(window).scrollTop(); //get the position of the containers var one = $("#one").offset().top, two = $("#two").offset().top, three = $("#three").offset().top; var nav1 = $("#nav1"), nav2 = $("#nav2"), nav3 = $("#nav3"); //if the scroll position is the same as the position of the container specified, add the "active" class to the corresponding nav element if (scrollPosition >= one) { nav1.siblings().removeClass("active"); nav1.addClass("active"); } if (scrollPosition >= two) { nav2.siblings().removeClass("active"); nav2.addClass("active"); } if (scrollPosition >= three) { nav3.siblings().removeClass("active"); nav3.addClass("active"); } }); /*-------------------------------------------------------- //add active class to the clicked element --------------------------------------------------------*/ $("a").click(function() { $(this).siblings().removeClass("active"); $(this).addClass("active"); }); /*------------------------------------------------------ smooth-scrolling; taken from https://css-tricks.com/snippets/jquery/smooth-scrolling/ -------------------------------------------------------*/ // Select all links with hashes $('a[href*="#"]') // Remove links that don't actually link to anything .not('[href="#"]') .not('[href="#0"]') .click(function(event) { // On-page links if ( location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname ) { // Figure out element to scroll to var target = $(this.hash); target = target.length ? target : $('[name=' + this.hash.slice(1) + ']'); // Does a scroll target exist? if (target.length) { // Only prevent default if animation is actually gonna happen event.preventDefault(); $('html, body').animate({ scrollTop: target.offset().top }, 1000, function() { // Callback after animation // Must change focus! var $target = $(target); $target.focus(); if ($target.is(":focus")) { // Checking if the target was focused return false; } else { $target.attr('tabindex','-1'); // Adding tabindex for elements not focusable $target.focus(); // Set focus again }; }); } } }); });

Related: See More


Questions / Comments: