View on GitHub

Scrollspy

Automatically update Bootstrap navigation or list group components based on scroll position to indicate which link is currently active in the viewport.

How it works

Scrollspy toggles the .active class on anchor (<a>) elements when the element with the id referenced by the anchor’s href is scrolled into view. Scrollspy is best used in conjunction with a Bootstrap nav component or list group, but it will also work with any anchor elements in the current page. Here’s how it works.

  • To start, scrollspy requires two things: a navigation, list group, or a simple set of links, plus a scrollable container. The scrollable container can be the <body> or a custom element with a set height and overflow-y: scroll.

  • On the scrollable container, add data-bs-spy="scroll" and data-bs-target="#navId" where navId is the unique id of the associated navigation. Be sure to also include a tabindex="0" to ensure keyboard access.

  • As you scroll the “spied” container, an .active class is added and removed from anchor links within the associated navigation. Links must have resolvable id targets, otherwise they’re ignored. For example, a <a href="#home">home</a> must correspond to something in the DOM like <div id="home"></div>

  • Target elements that are not visible will be ignored. See the Non-visible elements section below.

Examples

Navbar

Scroll the area below the navbar and watch the active class change. Open the dropdown menu and watch the dropdown items be highlighted as well.

Scrollspy - 图1

  1. <nav id="navbar-example2" class="navbar bg-light px-3 mb-3">
  2. <a class="navbar-brand" href="#">Navbar</a>
  3. <ul class="nav nav-pills">
  4. <li class="nav-item">
  5. <a class="nav-link" href="#scrollspyHeading1">First</a>
  6. </li>
  7. <li class="nav-item">
  8. <a class="nav-link" href="#scrollspyHeading2">Second</a>
  9. </li>
  10. <li class="nav-item dropdown">
  11. <a class="nav-link dropdown-toggle" data-bs-toggle="dropdown" href="#" role="button" aria-expanded="false">Dropdown</a>
  12. <ul class="dropdown-menu">
  13. <li><a class="dropdown-item" href="#scrollspyHeading3">Third</a></li>
  14. <li><a class="dropdown-item" href="#scrollspyHeading4">Fourth</a></li>
  15. <li><hr class="dropdown-divider"></li>
  16. <li><a class="dropdown-item" href="#scrollspyHeading5">Fifth</a></li>
  17. </ul>
  18. </li>
  19. </ul>
  20. </nav>
  21. <div 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-light p-3 rounded-2" tabindex="0">
  22. <h4 id="scrollspyHeading1">First heading</h4>
  23. <p>...</p>
  24. <h4 id="scrollspyHeading2">Second heading</h4>
  25. <p>...</p>
  26. <h4 id="scrollspyHeading3">Third heading</h4>
  27. <p>...</p>
  28. <h4 id="scrollspyHeading4">Fourth heading</h4>
  29. <p>...</p>
  30. <h4 id="scrollspyHeading5">Fifth heading</h4>
  31. <p>...</p>
  32. </div>

Nested nav

Scrollspy also works with nested .navs. If a nested .nav is .active, its parents will also be .active. Scroll the area next to the navbar and watch the active class change.

Scrollspy - 图2

  1. <div class="row">
  2. <div class="col-4">
  3. <nav id="navbar-example3" class="h-100 flex-column align-items-stretch pe-4 border-end">
  4. <nav class="nav nav-pills flex-column">
  5. <a class="nav-link" href="#item-1">Item 1</a>
  6. <nav class="nav nav-pills flex-column">
  7. <a class="nav-link ms-3 my-1" href="#item-1-1">Item 1-1</a>
  8. <a class="nav-link ms-3 my-1" href="#item-1-2">Item 1-2</a>
  9. </nav>
  10. <a class="nav-link" href="#item-2">Item 2</a>
  11. <a class="nav-link" href="#item-3">Item 3</a>
  12. <nav class="nav nav-pills flex-column">
  13. <a class="nav-link ms-3 my-1" href="#item-3-1">Item 3-1</a>
  14. <a class="nav-link ms-3 my-1" href="#item-3-2">Item 3-2</a>
  15. </nav>
  16. </nav>
  17. </nav>
  18. </div>
  19. <div class="col-8">
  20. <div data-bs-spy="scroll" data-bs-target="#navbar-example3" data-bs-smooth-scroll="true" class="scrollspy-example-2" tabindex="0">
  21. <div id="item-1">
  22. <h4>Item 1</h4>
  23. <p>...</p>
  24. </div>
  25. <div id="item-1-1">
  26. <h5>Item 1-1</h5>
  27. <p>...</p>
  28. </div>
  29. <div id="item-1-2">
  30. <h5>Item 1-2</h5>
  31. <p>...</p>
  32. </div>
  33. <div id="item-2">
  34. <h4>Item 2</h4>
  35. <p>...</p>
  36. </div>
  37. <div id="item-3">
  38. <h4>Item 3</h4>
  39. <p>...</p>
  40. </div>
  41. <div id="item-3-1">
  42. <h5>Item 3-1</h5>
  43. <p>...</p>
  44. </div>
  45. <div id="item-3-2">
  46. <h5>Item 3-2</h5>
  47. <p>...</p>
  48. </div>
  49. </div>
  50. </div>
  51. </div>

List group

Scrollspy also works with .list-groups. Scroll the area next to the list group and watch the active class change.

Scrollspy - 图3

  1. <div class="row">
  2. <div class="col-4">
  3. <div id="list-example" class="list-group">
  4. <a class="list-group-item list-group-item-action" href="#list-item-1">Item 1</a>
  5. <a class="list-group-item list-group-item-action" href="#list-item-2">Item 2</a>
  6. <a class="list-group-item list-group-item-action" href="#list-item-3">Item 3</a>
  7. <a class="list-group-item list-group-item-action" href="#list-item-4">Item 4</a>
  8. </div>
  9. </div>
  10. <div class="col-8">
  11. <div data-bs-spy="scroll" data-bs-target="#list-example" data-bs-smooth-scroll="true" class="scrollspy-example" tabindex="0">
  12. <h4 id="list-item-1">Item 1</h4>
  13. <p>...</p>
  14. <h4 id="list-item-2">Item 2</h4>
  15. <p>...</p>
  16. <h4 id="list-item-3">Item 3</h4>
  17. <p>...</p>
  18. <h4 id="list-item-4">Item 4</h4>
  19. <p>...</p>
  20. </div>
  21. </div>
  22. </div>

Simple anchors

Scrollspy is not limited to nav components and list groups, so it will work on any <a> anchor elements in the current document. Scroll the area and watch the .active class change.

Scrollspy - 图4

  1. <div class="row">
  2. <div class="col-4">
  3. <div id="simple-list-example" class="d-flex flex-column gap-2 simple-list-example-scrollspy text-center">
  4. <a class="p-1 rounded" href="#simple-list-item-1">Item 1</a>
  5. <a class="p-1 rounded" href="#simple-list-item-2">Item 2</a>
  6. <a class="p-1 rounded" href="#simple-list-item-3">Item 3</a>
  7. <a class="p-1 rounded" href="#simple-list-item-4">Item 4</a>
  8. <a class="p-1 rounded" href="#simple-list-item-5">Item 5</a>
  9. </div>
  10. </div>
  11. <div class="col-8">
  12. <div data-bs-spy="scroll" data-bs-target="#simple-list-example" data-bs-offset="0" data-bs-smooth-scroll="true" class="scrollspy-example" tabindex="0">
  13. <h4 id="simple-list-item-1">Item 1</h4>
  14. <p>...</p>
  15. <h4 id="simple-list-item-2">Item 2</h4>
  16. <p>...</p>
  17. <h4 id="simple-list-item-3">Item 3</h4>
  18. <p>...</p>
  19. <h4 id="simple-list-item-4">Item 4</h4>
  20. <p>...</p>
  21. <h4 id="simple-list-item-5">Item 5</h4>
  22. <p>...</p>
  23. </div>
  24. </div>
  25. </div>

Non-visible elements

Target elements that aren’t visible will be ignored and their corresponding nav items won’t receive an .active class. Scrollspy instances initialized in a non-visible wrapper will ignore all target elements. Use the refresh method to check for observable elements once the wrapper becomes visible.

  1. document.querySelectorAll('#nav-tab>[data-bs-toggle="tab"]').forEach(el => {
  2. el.addEventListener('shown.bs.tab', () => {
  3. const target = el.getAttribute('data-bs-target')
  4. const scrollElem = document.querySelector(`${target} [data-bs-spy="scroll"]`)
  5. bootstrap.ScrollSpy.getOrCreateInstance(scrollElem).refresh()
  6. })
  7. })

Usage

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 name of the parent element of any Bootstrap .nav component.

  1. <body data-bs-spy="scroll" data-bs-target="#navbar-example">
  2. ...
  3. <div id="navbar-example">
  4. <ul class="nav nav-tabs" role="tablist">
  5. ...
  6. </ul>
  7. </div>
  8. ...
  9. </body>

Via JavaScript

  1. const scrollSpy = new bootstrap.ScrollSpy(document.body, {
  2. target: '#navbar-example'
  3. })

Options

As options can be passed via data attributes or JavaScript, you can append an option name to data-bs-, as in data-bs-animation="{value}". Make sure to change the case type of the option name from “camelCase” to “kebab-case” when passing the options via data attributes. For example, use data-bs-custom-class="beautifier" instead of data-bs-customClass="beautifier".

As of Bootstrap 5.2.0, all components support an experimental reserved data attribute data-bs-config that can house simple component configuration as a JSON string. When an element has data-bs-config='{"delay":0, "title":123}' and data-bs-title="456" attributes, the final title value will be 456 and the separate data attributes will override values given on data-bs-config. In addition, existing data attributes are able to house JSON values like data-bs-delay='{"show":0,"hide":150}'.

NameTypeDefaultDescription
rootMarginstring0px 0px -25%Intersection Observer rootMargin valid units, when calculating scroll position.
smoothScrollbooleanfalseEnables smooth scrolling when a user clicks on a link that refers to ScrollSpy observables.
targetstring, DOM elementnullSpecifies element to apply Scrollspy plugin.
thresholdarray[0.1, 0.5, 1]IntersectionObserver threshold valid input, when calculating scroll position.

Deprecated Options

Up until v5.1.3 we were using offset & method options, which are now deprecated and replaced by rootMargin. To keep backwards compatibility, we will continue to parse a given offset to rootMargin, but this feature will be removed in v6.

Methods

MethodDescription
disposeDestroys an element’s scrollspy. (Removes stored data on the DOM element)
getInstanceStatic method to get the scrollspy instance associated with a DOM element.
getOrCreateInstanceStatic method to get the scrollspy instance associated with a DOM element, or to create a new one in case it wasn’t initialized.
refreshWhen adding or removing elements in the DOM, you’ll need to call the refresh method.

Here’s an example using the refresh method:

  1. const dataSpyList = document.querySelectorAll('[data-bs-spy="scroll"]')
  2. dataSpyList.forEach(dataSpyEl => {
  3. bootstrap.ScrollSpy.getInstance(dataSpyEl).refresh()
  4. })

Events

EventDescription
activate.bs.scrollspyThis event fires on the scroll element whenever an anchor is activated by the scrollspy.
  1. const firstScrollSpyEl = document.querySelector('[data-bs-spy="scroll"]')
  2. firstScrollSpyEl.addEventListener('activate.bs.scrollspy', () => {
  3. // do something...
  4. })