Bootstrap Scrollspy
Bootstrap Scrollspy is a plugin that tracks the scrolling of a web page and highlights the active section in the navbar. This is useful for long webpages with multiple sections that require navigation.
Syntax
To use Scrollspy in your HTML file, you need to add the data-spy
and data-target
attributes to the navbar and the section divs. The data-spy
attribute should be set to "scroll" and the data-target
attribute should be set to the ID of the navbar.
<nav id="navbar-example2" class="navbar bg-body-tertiary px-3 mb-3">
<a class="navbar-brand" href="#">Navbar</a>
<ul class="nav nav-pills">
<li class="nav-item">
<a class="nav-link" href="#scrollspyHeading1">First</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#scrollspyHeading2">Second</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#scrollspyHeading3">Third</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#scrollspyHeading4">Fourth</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#scrollspyHeading5">Fifth</a>
</li>
</ul>
</nav>
<div style="height: 100px;" data-bs-spy="scroll" data-bs-target="#navbar-example2" data-bs-root-margin="0px 0px -40%"
data-bs-smooth-scroll="true" class="scrollspy-example bg-body-tertiary p-3 rounded-2" tabindex="0">
<h4 id="scrollspyHeading1">First heading</h4>
<p>...</p>
<h4 id="scrollspyHeading2">Second heading</h4>
<p>...</p>
<h4 id="scrollspyHeading3">Third heading</h4>
<p>...</p>
<h4 id="scrollspyHeading4">Fourth heading</h4>
<p>...</p>
<h4 id="scrollspyHeading5">Fifth heading</h4>
<p>...</p>
</div>