Back
Syntax
Study
Editor
Mode:
HTML
CSS
JavaScript
PHP
Reset
Run »
HTML / CSS / JS
const sections = $("section[id]"); const $navLinks = $(".sidebar-nav a"); $(window).on("scroll.spy", $.throttle(100, function() { const scrollPos = $(this).scrollTop() + 100; sections.each(function() { const top = $(this).offset().top; const bot = top + $(this).outerHeight(); if (scrollPos >= top && scrollPos < bot) { $navLinks.removeClass("active"); $navLinks.filter(`[href="#${this.id}"]`).addClass("active"); } }); }));
Result
Open