- It must be used on a Bootstrap or list group.
- Scrollspy requires on the element you’re spying on, usually the
<body>
. - Anchors (
<a>
) are required and must point to an element with thatid
.
When successfully implemented, your nav or list group will update accordingly, moving the .active
class from one item to the next based on their associated targets.
If you’re making a scrollable container (other than the <body>
), be sure to have a height
set and overflow-y: scroll;
applied to it—alongside a tabindex="0"
to ensure keyboard access.
Scroll the area below the navbar and watch the active class change. The dropdown items will be highlighted as well.
<nav id="navbar-example3" class="navbar navbar-light bg-light">
<a class="navbar-brand" href="#">Navbar</a>
<nav class="nav nav-pills flex-column">
<a class="nav-link" href="#item-1">Item 1</a>
<nav class="nav nav-pills flex-column">
<a class="nav-link ms-3 my-1" href="#item-1-1">Item 1-1</a>
<a class="nav-link ms-3 my-1" href="#item-1-2">Item 1-2</a>
</nav>
<a class="nav-link" href="#item-2">Item 2</a>
<nav class="nav nav-pills flex-column">
<a class="nav-link ms-3 my-1" href="#item-3-1">Item 3-1</a>
<a class="nav-link ms-3 my-1" href="#item-3-2">Item 3-2</a>
</nav>
</nav>
</nav>
<div data-bs-spy="scroll" data-bs-target="#navbar-example3" data-bs-offset="0" tabindex="0">
<h4 id="item-1">Item 1</h4>
<p>...</p>
<h5 id="item-1-1">Item 1-1</h5>
<h5 id="item-1-2">Item 1-2</h5>
<p>...</p>
<h4 id="item-2">Item 2</h4>
<p>...</p>
<h4 id="item-3">Item 3</h4>
<p>...</p>
<h5 id="item-3-1">Item 3-1</h5>
<p>...</p>
<h5 id="item-3-2">Item 3-2</h5>
<p>...</p>
</div>
Scrollspy also works with .list-group
s. Scroll the area next to the list group and watch the active class change.
<div id="list-example" class="list-group">
<a class="list-group-item list-group-item-action" href="#list-item-1">Item 1</a>
<a class="list-group-item list-group-item-action" href="#list-item-2">Item 2</a>
<a class="list-group-item list-group-item-action" href="#list-item-3">Item 3</a>
</div>
<div data-bs-spy="scroll" data-bs-target="#list-example" data-bs-offset="0" class="scrollspy-example" tabindex="0">
<h4 id="list-item-1">Item 1</h4>
<p>...</p>
<h4 id="list-item-2">Item 2</h4>
<p>...</p>
<h4 id="list-item-3">Item 3</h4>
<p>...</p>
<h4 id="list-item-4">Item 4</h4>
<p>...</p>
</div>
Via data attributes
To easily add scrollspy behavior to your topbar navigation, add data-bs-spy="scroll"
to the element you want to spy on (most typically this would be the <body>
). Then add the data-bs-target
attribute with the ID or class of the parent element of any Bootstrap .nav
component.
<body data-bs-spy="scroll" data-bs-target="#navbar-example">
<div id="navbar-example">
<ul class="nav nav-tabs" role="tablist">
...
</ul>
</div>
...
</body>
var scrollSpy = new bootstrap.ScrollSpy(document.body, {
target: '#navbar-example'
})
Resolvable ID targets required
Navbar links must have resolvable id targets. For example, a <a href="#home">home</a>
must correspond to something in the DOM like <div id="home"></div>
.
Non-visible target elements ignored
Target elements that are not visible will be ignored and their corresponding nav items will never be highlighted.
Methods
refresh
When using scrollspy in conjunction with adding or removing of elements from the DOM, you’ll need to call the refresh method like so:
dispose
Destroys an element’s scrollspy. (Removes stored data on the DOM element)
getInstance
var scrollSpyContentEl = document.getElementById('content')
var scrollSpy = bootstrap.ScrollSpy.getInstance(scrollSpyContentEl) // Returns a Bootstrap scrollspy instance
Options can be passed via data attributes or JavaScript. For data attributes, append the option name to data-bs-
, as in data-bs-offset=""
.
Events
Event type | Description |
---|---|
activate.bs.scrollspy | This event fires on the scroll element whenever a new item becomes activated by the scrollspy. |
var firstScrollSpyEl = document.querySelector('[data-bs-spy="scroll"]')
firstScrollSpyEl.addEventListener('activate.bs.scrollspy', function () {
})