📅  最后修改于: 2023-12-03 15:34:52.745000             🧑  作者: Mango
Scrollspy is a JavaScript plugin included in Bootstrap 4 that highlights the active section of a webpage or navigation menu based on the scroll position. It enables users to easily navigate long pages and improves the overall user experience on the website.
To use scrollspy in your website, you need to follow these steps:
<nav id="navbar-example2" class="navbar navbar-light bg-light">
<a class="navbar-brand" href="#">Navbar</a>
<ul class="nav nav-pills">
<li class="nav-item">
<a class="nav-link" href="#item-1">Item 1</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#item-2">Item 2</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#item-3">Item 3</a>
</li>
</ul>
</nav>
In this example, the data-spy attribute is added to the navbar element.
<div id="item-1">Item 1 content</div>
<div id="item-2">Item 2 content</div>
<div id="item-3">Item 3 content</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/js/bootstrap.min.js"></script>
$('body').scrollspy({ target: '#navbar-example2' })
Scrollspy is a useful Bootstrap 4 plugin that makes it easy to navigate long pages and improve the user experience on your website. By highlighting the active section in the navigation menu, users can easily see where they are on the page and where they can go next. With just a few lines of code, you can add scrollspy to your website and enhance its functionality.