Scroll-Spy Side Nav
A side nav that highlights the active section as you scroll.
Requires Bootstrap 5. Add the CDN tags below to your page (styles in
<head>, scripts before </body>). The downloaded file already includes them.<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"><script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script><div class="card border-0 shadow-sm" style="max-width:340px"><div class="card-body p-0 d-flex">
<div class="border-end p-2" id="ssNav"><a class="d-block small py-1 px-2 rounded text-decoration-none active" data-ss="0">Intro</a><a class="d-block small py-1 px-2 rounded text-decoration-none" data-ss="1">Setup</a><a class="d-block small py-1 px-2 rounded text-decoration-none" data-ss="2">Usage</a></div>
<div class="p-3 overflow-auto" id="ssBody" style="height:160px"><h6 data-sec="0">Intro</h6><p class="small text-secondary">Welcome to the guide.</p><div style="height:60px"></div><h6 data-sec="1">Setup</h6><p class="small text-secondary">Install and configure.</p><div style="height:60px"></div><h6 data-sec="2">Usage</h6><p class="small text-secondary">Daily usage tips.</p><div style="height:80px"></div></div>
</div></div>var body=document.getElementById('ssBody'),navs=document.querySelectorAll('#ssNav [data-ss]'),secs=body.querySelectorAll('[data-sec]');
navs.forEach(function(a){a.onclick=function(){secs[+a.dataset.ss].scrollIntoView({behavior:'smooth',block:'start'})}});
body.addEventListener('scroll',function(){var top=body.scrollTop,active=0;secs.forEach(function(s,k){if(s.offsetTop-body.offsetTop<=top+10)active=k});navs.forEach(function(a,k){a.classList.toggle('active',k===active);a.classList.toggle('bg-primary',k===active);a.classList.toggle('text-white',k===active)})});Free to use.This component uses Bootstrap 5 (linked above). Copy any file, or download a ready-to-open .html page with the CDN links already included.