![]() |
VOOZH | about |
Bootstrap 5 Scrollspy Via data attributes facilitate adding the scrollspy behavior in the navigation. This will only work if we add the data-bs-spy attribute to the element that is to spy, along with including the data-bs-target attribute, specifying the id or class name of the parent element of the div container or the nav component.
Bootstrap 5 Scrollspy Via data attributes:
Syntax:
<body data-bs-spy="scroll"
data-bs-target="selectorID/selectorCLASS">
...
</body>
Example 1: The below code example demonstrates how we can implement the Scrollspy using Data Attributes in a Navbar.
Output:
Example 2: This is another code example that demonstrates how we can implement the Scrollspy using Data Attributes in a Nested Side Navbar.
Output:
Reference: https://getbootstrap.com/docs/5.0/components/scrollspy/#via-data-attributes