"on scroll section color change with active class"
Bootstrap 4.1.1 Snippet by Poojathakur

<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 ----------> <!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <title>CodePen - Highlight Active Nav Link On Page Scroll (Plain JS)</title> <link rel="stylesheet" href="./style.css"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <!-- partial:index.partial.html --> <div class="row first-cl"> <div class="col-lg-6 side-bar"> <nav class="navigation"> <ul> <li> <a href="#about">About</a> <a href="#products">Products</a> <a href="#services">Services</a> <a href="#contact">Contact</a> <a href="#gallery">Gallery</a> </li> </ul> </nav> </div> <div class="col-lg-6 right-section"> <section id="about"> <h1>About</h1> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> </section> <section id="products"> <h1>Products</h1> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> </section> <section id="services"> <h1>Services</h1> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> </section> <section id="contact"> <h1>Contact</h1> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> </section> <section id="gallery"> <h1>Gallery</h1> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> </section> </div> </div> <section> <div class="container"> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> </div> </section> <!-- partial --> <script src='https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js'></script> <script src='https://cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.7/ScrollMagic.min.js'></script> </body> </html>
@import url('https://fonts.googleapis.com/css?family=Montserrat:400,600'); * { box-sizing: border-box } html { scroll-behavior: smooth; } body { font-family: 'Montserrat', sans-serif; margin: 0; overflow-x: hidden; } img { width: 100%; } .side-bar { height: 100vh; position: sticky; display: flex; flex-direction: column; align-items: center; justify-content: center; top: 0; left: 0; } nav.navigation ul li a { color: #fff; font-size: 20px; margin-bottom: 20px; font-weight: 800; } nav ul { padding: 0; } nav li { list-style: none; } nav li a { text-decoration: none; color: #333; display: block; padding: 0.6rem 0.3rem; font-weight: 400; transition: all ease-out 250ms; } nav li a:hover { color: lime; } .main-content { width: 50%; float: right; } .first-cl{ position: relative; } #about, #products , #services , #contact , #gallery{ padding: 30px; } .main-content section { background-color: #f2f2f2; margin: 0; padding: 2.5rem 4rem; } .black-bg{ background-color: #000; } .red-bg{ background-color: #b50101; } .blue-bg{ background-color: #e68500; } .yellow-bg{ background-color: #e3b700; } .grren{ background-color: #417d34; } .navy-bg{ background-color: #010a6b; } .main-content section:last-of-type { height: 100vh; } .next { position: relative; } h1 { color: #444; font-size: 40px; font-weight: 900; } nav.navigation ul li a.active { color: #000; font-weight: 600; background: #fff; width: 100%; font-size: 16px; text-align: center; border-radius: 5px; padding: 10px 20px; } p { font-size: 18px; line-height: 1.7em; } .right-section{ height: 100%; } .right-section section { height: 100vh; display: flex; flex-direction: column; justify-content: center; }
$(document).ready(function() { var controller = new ScrollMagic.Controller() var second = new ScrollMagic.Scene({ triggerElement: '#about', duration: "100%", }) .setClassToggle('.side-bar', 'black-bg') .addTo(controller); var third = new ScrollMagic.Scene({ triggerElement: '#products', duration: "100%", }) .setClassToggle('.side-bar', 'red-bg') .addTo(controller); var fourth = new ScrollMagic.Scene({ triggerElement: '#services', duration: "100%", }) .setClassToggle('.side-bar', 'blue-bg') .addTo(controller); var fifth = new ScrollMagic.Scene({ triggerElement: '#contact', duration: "100%", }) .setClassToggle('.side-bar', 'yellow-bg') .addTo(controller); var sixth = new ScrollMagic.Scene({ triggerElement: '#gallery', duration: "100%", }) .setClassToggle('.side-bar', 'navy-bg') .addTo(controller); }) // Get all sections that have an ID defined const sections = document.querySelectorAll("section[id]"); // Add an event listener listening for scroll window.addEventListener("scroll", navHighlighter); function navHighlighter() { // Get current scroll position let scrollY = window.pageYOffset; // Now we loop through sections to get height, top and ID values for each sections.forEach(current => { const sectionHeight = current.offsetHeight; const sectionTop = current.offsetTop - 50; sectionId = current.getAttribute("id"); /* - If our current scroll position enters the space where current section on screen is, add .active class to corresponding navigation link, else remove it - To know which link needs an active class, we use sectionId variable we are getting while looping through sections as an selector */ if ( scrollY > sectionTop && scrollY <= sectionTop + sectionHeight ){ document.querySelector(".navigation a[href*=" + sectionId + "]").classList.add("active"); } else { document.querySelector(".navigation a[href*=" + sectionId + "]").classList.remove("active"); } }); }

Related: See More


Questions / Comments: