Update the active link of a nav or a list as the sections it points to scroll into view, with the Scrollspy plugin.
Introduction
Scrollspy updates the active link of a navigation as the content scrolls. Each link points to a section by its id, and the plugin adds .active to the link whose section is in view and removes it from the others.
How it works
The plugin watches the sections with the Intersection Observer API instead of listening to scroll events. It pairs every link inside the target with the element its href hash names, and observes that element within the scrollable container.
The active state also reaches the parents of a link. In nested .nav elements, the .nav-link before the nested nav becomes active with it, and a .menu-item activates the toggle of its menu.
Chassis CSS respects user accessibility preferences by automatically disabling animations when the prefers-reduced-motion media query is detected. See the reduced motion guidelines in the accessibility documentation for implementation details.
Basic structure
Scrollspy needs a scrollable container and a navigation whose links point to elements inside it:
- The scrollable container is the
<body>element, or an element with a setheightandoverflow-y: scroll. - Add
data-cx-spy="scroll"to the container, anddata-cx-targetwith a selector for the element that holds the links. - Add
tabindex="0"to a container other than<body>when it holds no focusable element, so keyboard users can scroll it. - Each link needs an
hrefwith theidof an element inside the container. Links that are disabled or that point to a missing or hidden element are ignored.
Navbar
The example pairs a navbar with a scrollable region. Links in the nav and in its menu become active as their sections scroll into view. The data-cx-root-margin and data-cx-smooth-scroll attributes set two of the options.
First heading
This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.
Second heading
This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.
Third heading
This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.
Fourth heading
This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.
Fifth heading
This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.
<nav id="navbar-example" class="navbar bg-evident px-md mb-md" aria-label="Scrollspy example navigation">
<a class="navbar-brand" href="#">Navbar</a>
<ul class="nav nav-segments">
<li class="nav-item">
<a class="nav-link" href="#scrollspyHeading1">First</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#scrollspyHeading2">Second</a>
</li>
<li class="nav-item dropdown">
<button type="button" class="nav-link caret" data-cx-toggle="menu" aria-expanded="false">Dropdown</button>
<ul class="menu">
<li><a class="menu-item" href="#scrollspyHeading3">Third</a></li>
<li><a class="menu-item" href="#scrollspyHeading4">Fourth</a></li>
<li><hr class="menu-divider"></li>
<li><a class="menu-item" href="#scrollspyHeading5">Fifth</a></li>
</ul>
</li>
</ul>
</nav>
<div data-cx-spy="scroll" data-cx-target="#navbar-example" data-cx-root-margin="0px 0px -40%" data-cx-smooth-scroll="true" class="scrollspy-example bg-evident p-md rounded-sm" tabindex="0">
<h4 id="scrollspyHeading1">First heading</h4>
<p>...</p>
<h4 id="scrollspyHeading2">Second heading</h4>
<p>...</p>
<h4 id="scrollspyHeading3">Third heading</h4>
<p>...</p>
<h4 id="scrollspyHeading4">Fourth heading</h4>
<p>...</p>
<h4 id="scrollspyHeading5">Fifth heading</h4>
<p>...</p>
</div>Navigation patterns
Scrollspy follows any set of anchors, not only a navbar: nested navs, lists, and plain links are tracked the same way.
Nested navigation
Scrollspy works with nested .nav elements. When a link of a nested nav becomes active, the link before that nav, its parent in the hierarchy, becomes active too.
Item 1
This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.
Keep in mind that the JavaScript plugin tries to pick the right element among all that may be visible. Multiple visible scrollspy targets at the same time may cause some issues.
Item 1-1
This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.
Keep in mind that the JavaScript plugin tries to pick the right element among all that may be visible. Multiple visible scrollspy targets at the same time may cause some issues.
Item 1-2
This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.
Keep in mind that the JavaScript plugin tries to pick the right element among all that may be visible. Multiple visible scrollspy targets at the same time may cause some issues.
Item 2
This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.
Keep in mind that the JavaScript plugin tries to pick the right element among all that may be visible. Multiple visible scrollspy targets at the same time may cause some issues.
Item 3
This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.
Keep in mind that the JavaScript plugin tries to pick the right element among all that may be visible. Multiple visible scrollspy targets at the same time may cause some issues.
Item 3-1
This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.
Keep in mind that the JavaScript plugin tries to pick the right element among all that may be visible. Multiple visible scrollspy targets at the same time may cause some issues.
Item 3-2
This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.
Keep in mind that the JavaScript plugin tries to pick the right element among all that may be visible. Multiple visible scrollspy targets at the same time may cause some issues.
<div class="grid">
<div class="col-span-4">
<nav id="nested-example" class="h-100 flex-column align-items-stretch pe-lg border-end" aria-label="Nested scrollspy navigation">
<nav class="nav nav-segments flex-column rounded-3xl" aria-label="Main sections">
<a class="nav-link" href="#item-1">Item 1</a>
<nav class="nav nav-segments flex-column" aria-label="Item 1 subsections">
<a class="nav-link ms-md my-2xs" href="#item-1-1">Item 1-1</a>
<a class="nav-link ms-md my-2xs" href="#item-1-2">Item 1-2</a>
</nav>
<a class="nav-link" href="#item-2">Item 2</a>
<a class="nav-link" href="#item-3">Item 3</a>
<nav class="nav nav-segments flex-column" aria-label="Item 3 subsections">
<a class="nav-link ms-md my-2xs" href="#item-3-1">Item 3-1</a>
<a class="nav-link ms-md my-2xs" href="#item-3-2">Item 3-2</a>
</nav>
</nav>
</nav>
</div>
<div class="col-span-8">
<div data-cx-spy="scroll" data-cx-target="#nested-example" data-cx-smooth-scroll="true" class="scrollspy-example-2" tabindex="0">
<div id="item-1">
<h4>Item 1</h4>
<p>...</p>
</div>
<div id="item-1-1">
<h5>Item 1-1</h5>
<p>...</p>
</div>
<div id="item-1-2">
<h5>Item 1-2</h5>
<p>...</p>
</div>
<div id="item-2">
<h4>Item 2</h4>
<p>...</p>
</div>
<div id="item-3">
<h4>Item 3</h4>
<p>...</p>
</div>
<div id="item-3-1">
<h5>Item 3-1</h5>
<p>...</p>
</div>
<div id="item-3-2">
<h5>Item 3-2</h5>
<p>...</p>
</div>
</div>
</div>
</div>Lists
Scrollspy also works with a list whose .list-item elements are anchors.
Item 1
This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.
Item 2
This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.
Item 3
This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.
Item 4
This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.
<div class="grid">
<div class="col-span-4">
<div id="list-example" class="list">
<a class="list-item list-action" href="#list-item-1">Item 1</a>
<a class="list-item list-action" href="#list-item-2">Item 2</a>
<a class="list-item list-action" href="#list-item-3">Item 3</a>
<a class="list-item list-action" href="#list-item-4">Item 4</a>
</div>
</div>
<div class="col-span-8">
<div data-cx-spy="scroll" data-cx-target="#list-example" data-cx-smooth-scroll="true" 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>
</div>
</div>Custom anchors
The plugin does not require a nav or a list: it tracks any anchor with an href inside the target element. Style the .active class of such anchors in the project's own CSS.
Item 1
This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.
Item 2
This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.
Item 3
This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.
Item 4
This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.
Item 5
This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.
<div class="grid">
<div class="col-span-4">
<div id="simple-list-example" class="d-flex flex-column gap-xs simple-list-example-scrollspy text-center">
<a class="p-2xs rounded" href="#simple-list-item-1">Item 1</a>
<a class="p-2xs rounded" href="#simple-list-item-2">Item 2</a>
<a class="p-2xs rounded" href="#simple-list-item-3">Item 3</a>
<a class="p-2xs rounded" href="#simple-list-item-4">Item 4</a>
<a class="p-2xs rounded" href="#simple-list-item-5">Item 5</a>
</div>
</div>
<div class="col-span-8">
<div data-cx-spy="scroll" data-cx-target="#simple-list-example" data-cx-offset="0" data-cx-smooth-scroll="true" class="scrollspy-example" tabindex="0">
<h4 id="simple-list-item-1">Item 1</h4>
<p>...</p>
<h4 id="simple-list-item-2">Item 2</h4>
<p>...</p>
<h4 id="simple-list-item-3">Item 3</h4>
<p>...</p>
<h4 id="simple-list-item-4">Item 4</h4>
<p>...</p>
<h4 id="simple-list-item-5">Item 5</h4>
<p>...</p>
</div>
</div>
</div>Non-visible elements
Target elements that are not visible are ignored, and their links never receive .active. An instance initialized in a non-visible wrapper ignores all of its targets. Call the refresh method once the wrapper becomes visible, for example when a tab pane with a scrollspy container is shown:
import { ScrollSpy } from '@chassis-ui/css'
document.querySelectorAll('#nav-tab>[data-cx-toggle="tab"]').forEach(el => {
el.addEventListener('shown.cx.tab', () => {
const target = el.getAttribute('data-cx-target')
const scrollElem = document.querySelector(`${target} [data-cx-spy="scroll"]`)
ScrollSpy.getOrCreateInstance(scrollElem).refresh()
})
})Accessibility
The plugin changes only the .active class of the links and sets no ARIA attribute, so the active section is conveyed visually only. To expose it to assistive technologies, set aria-current="true" on the link in an activate.cx.scrollspy listener, where the link is event.relatedTarget, and remove the attribute from the link that was active before.
The links stay native anchors: they are focusable and activate with Enter whether or not the plugin runs. A scrollable container without a focusable element needs tabindex="0", otherwise keyboard users cannot focus it to scroll its content.
JavaScript API
The ScrollSpy plugin observes the sections of a scrollable container and updates the links of its target. Import the ScrollSpy class from the Chassis module:
import { ScrollSpy } from '@chassis-ui/css'Triggers
The data-attribute API sets up scrollspy without any JavaScript on the page beyond the bundled plugin. Add data-cx-spy="scroll" to the scrollable element, typically the <body> or a scrollable container, and data-cx-target with a selector for the parent element of the navigation:
<body data-cx-spy="scroll" data-cx-target="#navbar-example">
...
<div id="navbar-example">
<ul class="nav nav-tabs">
...
</ul>
</div>
...
</body>Initialization
For content added after the page has loaded, or to pass options as an object, instantiate the scrollable element with the ScrollSpy class:
const scrollSpy = new ScrollSpy(document.body, {
target: '#navbar-example'
})Options
Options are set via data-cx-* attributes or passed to the constructor as an object. Attribute names use the kebab-case form of the option name — data-cx-custom-class, not data-cx-customClass. Attribute values are parsed to their native types:"true" → true, "0" → 0, and valid JSON strings to objects.
Use data-cx-config to pass multiple options as a JSON string:data-cx-config='{"delay":200}'. Individual data-cx-* attributes take precedence over data-cx-config. Individual attributes accept JSON values too, such as data-cx-delay='{"show":100,"hide":200}'.
When initializing components, Chassis merges configurations from multiple sources in this priority order: default settings, data-cx-config values, individual data-cx-*attributes, and finally any JavaScript object options. Values defined later in this sequence override earlier ones.
| Name | Type | Default | Description |
|---|---|---|---|
rootMargin | string | 0px 0px -25% | rootMargin of the IntersectionObserver that detects the visible sections. |
smoothScroll | boolean | false | Scrolls smoothly to a section when its link is clicked. |
target | string, DOM element | null | Element that holds the links to update. Falls back to the <body> element when it is not set. |
threshold | array | [0.1, 0.5, 1] | threshold of the IntersectionObserver. A comma-separated string, such as "0.1,0.5,1", is also accepted. |
Methods
The plugin exposes two instance methods and two static lookup methods for programmatic control:
| Method | Description |
|---|---|
dispose | Destroys the instance and removes stored data from the element. |
getInstance | Static — returns the existing instance bound to a DOM element, or null. Example: ScrollSpy.getInstance(element) |
getOrCreateInstance | Static — returns the existing instance or creates one. Example: ScrollSpy.getOrCreateInstance(element) |
refresh | Collects the links and their sections again. Call it after adding or removing links or sections in the DOM, or after a hidden container becomes visible. |
A call to refresh on every instance picks up content that was added to the page:
const dataSpyList = document.querySelectorAll('[data-cx-spy="scroll"]')
dataSpyList.forEach(dataSpyEl => {
ScrollSpy.getInstance(dataSpyEl).refresh()
})Events
The plugin fires one event on the scrollable element, for integrating the active link with other parts of the page.
| Event | Description |
|---|---|
activate.cx.scrollspy | Fired on the scroll element whenever a link becomes active. event.relatedTarget is that link. |
const firstScrollSpyEl = document.querySelector('[data-cx-spy="scroll"]')
firstScrollSpyEl.addEventListener('activate.cx.scrollspy', event => {
event.relatedTarget // newly activated link
})