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 has a few requirements to function properly:

  • If you’re building our JavaScript from source, it requires util.js.
  • It must be used on a Bootstrap nav component or list group.
  • Scrollspy requires position: relative; on the element you’re spying on, usually the <body>.
  • When spying on elements other than the <body>, be sure to have a height set and overflow-y: scroll; applied.
  • Anchors (<a>) are required and must point to an element with that id.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.

Example in navbar

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

Scrollspy - 图1

  1. <nav id="navbar-example2" class="navbar navbar-light bg-light">
  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="#fat">@fat</a>
  6. </li>
  7. <li class="nav-item">
  8. <a class="nav-link" href="#mdo">@mdo</a>
  9. </li>
  10. <li class="nav-item dropdown">
  11. <a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">Dropdown</a>
  12. <div class="dropdown-menu">
  13. <a class="dropdown-item" href="#one">one</a>
  14. <a class="dropdown-item" href="#two">two</a>
  15. <div role="separator" class="dropdown-divider"></div>
  16. <a class="dropdown-item" href="#three">three</a>
  17. </div>
  18. </li>
  19. </ul>
  20. </nav>
  21. <div data-spy="scroll" data-target="#navbar-example2" data-offset="0">
  22. <h4 id="fat">@fat</h4>
  23. <p>...</p>
  24. <h4 id="mdo">@mdo</h4>
  25. <p>...</p>
  26. <h4 id="one">one</h4>
  27. <p>...</p>
  28. <h4 id="two">two</h4>
  29. <p>...</p>
  30. <h4 id="three">three</h4>
  31. <p>...</p>
  32. </div>

Example with 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. <nav id="navbar-example3" class="navbar navbar-light bg-light">
  2. <a class="navbar-brand" href="#">Navbar</a>
  3. <nav class="nav nav-pills flex-column">
  4. <a class="nav-link" href="#item-1">Item 1</a>
  5. <nav class="nav nav-pills flex-column">
  6. <a class="nav-link ml-3 my-1" href="#item-1-1">Item 1-1</a>
  7. <a class="nav-link ml-3 my-1" href="#item-1-2">Item 1-2</a>
  8. </nav>
  9. <a class="nav-link" href="#item-2">Item 2</a>
  10. <a class="nav-link" href="#item-3">Item 3</a>
  11. <nav class="nav nav-pills flex-column">
  12. <a class="nav-link ml-3 my-1" href="#item-3-1">Item 3-1</a>
  13. <a class="nav-link ml-3 my-1" href="#item-3-2">Item 3-2</a>
  14. </nav>
  15. </nav>
  16. </nav>
  17. <div data-spy="scroll" data-target="#navbar-example3" data-offset="0">
  18. <h4 id="item-1">Item 1</h4>
  19. <p>...</p>
  20. <h5 id="item-1-1">Item 1-1</h5>
  21. <p>...</p>
  22. <h5 id="item-1-2">Item 1-2</h5>
  23. <p>...</p>
  24. <h4 id="item-2">Item 2</h4>
  25. <p>...</p>
  26. <h4 id="item-3">Item 3</h4>
  27. <p>...</p>
  28. <h5 id="item-3-1">Item 3-1</h5>
  29. <p>...</p>
  30. <h5 id="item-3-2">Item 3-2</h5>
  31. <p>...</p>
  32. </div>

Example with 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 id="list-example" class="list-group">
  2. <a class="list-group-item list-group-item-action" href="#list-item-1">Item 1</a>
  3. <a class="list-group-item list-group-item-action" href="#list-item-2">Item 2</a>
  4. <a class="list-group-item list-group-item-action" href="#list-item-3">Item 3</a>
  5. <a class="list-group-item list-group-item-action" href="#list-item-4">Item 4</a>
  6. </div>
  7. <div data-spy="scroll" data-target="#list-example" data-offset="0" class="scrollspy-example">
  8. <h4 id="list-item-1">Item 1</h4>
  9. <p>...</p>
  10. <h4 id="list-item-2">Item 2</h4>
  11. <p>...</p>
  12. <h4 id="list-item-3">Item 3</h4>
  13. <p>...</p>
  14. <h4 id="list-item-4">Item 4</h4>
  15. <p>...</p>
  16. </div>

Usage

Via data attributes

To easily add scrollspy behavior to your topbar navigation, add data-spy="scroll" to the element you want to spy on (most typically this would be the <body>). Then add the data-target attribute with the ID or class of the parent element of any Bootstrap .nav component.

  1. body {
  2. position: relative;
  3. }
  1. <body data-spy="scroll" data-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

After adding position: relative; in your CSS, call the scrollspy via JavaScript:

  1. $('body').scrollspy({ 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 according to jQuery will be ignored and their corresponding nav items will never be highlighted.

Methods

.scrollspy('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:

  1. $('[data-spy="scroll"]').each(function () {
  2. var $spy = $(this).scrollspy('refresh')
  3. })

.scrollspy('dispose')

Destroys an element’s scrollspy.

Options

Options can be passed via data attributes or JavaScript. For data attributes, append the option name to data-, as in data-offset="".

NameTypeDefaultDescription
offsetnumber10Pixels to offset from top when calculating position of scroll.
methodstringautoFinds which section the spied element is in. auto will choose the best method get scroll coordinates. offset will use jQuery offset method to get scroll coordinates. position will use jQuery position method to get scroll coordinates.
targetstringSpecifies element to apply Scrollspy plugin.

Events

Event TypeDescription
activate.bs.scrollspyThis event fires on the scroll element whenever a new item becomes activated by the scrollspy.
  1. $('[data-spy="scroll"]').on('activate.bs.scrollspy', function () {
  2. // do something…
  3. })