Scrollspy
Highlights the navigation link of the section scrolled into view.
Bootstrap’s scrollspy docs describe the markup and CSS.
Example
Put bsScrollspy on the element that holds the sections and pass it the element that holds the links. Each link’s href is # and a section id. The active link gets .active and aria-current="true". Clicking a link scrolls to its section without leaving the page; add smoothScroll to animate it.
Section one
This is some placeholder content for the scrollspy page. As you scroll, the navigation highlights the section in view. Keep scrolling to see more sections.
Scrollspy uses an activation line near the top of the scroll area. The last section whose top has passed it is the active one.
Section two
This is some placeholder content for the scrollspy page. As you scroll, the navigation highlights the section in view. Keep scrolling to see more sections.
Scrollspy uses an activation line near the top of the scroll area. The last section whose top has passed it is the active one.
Section three
This is some placeholder content for the scrollspy page. As you scroll, the navigation highlights the section in view. Keep scrolling to see more sections.
Scrollspy uses an activation line near the top of the scroll area. The last section whose top has passed it is the active one.
Section four
This is some placeholder content for the scrollspy page. As you scroll, the navigation highlights the section in view. Keep scrolling to see more sections.
Scrollspy uses an activation line near the top of the scroll area. The last section whose top has passed it is the active one.
Active section:
<div class="row">
<div class="col-4">
<nav #toc bsNav variant="pills" vertical aria-label="Sections">
<a bsNavLink current="true" href="#spy-one">One</a>
<a bsNavLink current="true" href="#spy-two">Two</a>
<a bsNavLink current="true" href="#spy-three">Three</a>
<a bsNavLink current="true" href="#spy-four">Four</a>
</nav>
</div>
<div class="col-8">
<div
[bsScrollspy]="toc"
smoothScroll
(activate)="active.set($event)"
class="overflow-y-auto position-relative"
style="height: 240px"
tabindex="0"
role="region"
aria-label="Scrollspy content">
@for (section of ['one', 'two', 'three', 'four']; track section) {
<section [id]="`spy-${section}`" class="mb-4">
<h3 class="h5 text-capitalize">Section {{ section }}</h3>
<p>
This is some placeholder content for the scrollspy page. As you scroll, the navigation
highlights the section in view. Keep scrolling to see more sections.
</p>
<p>
Scrollspy uses an activation line near the top of the scroll area. The last section
whose top has passed it is the active one.
</p>
</section>
}
</div>
</div>
</div>
<p class="mt-2 mb-0">Active section: {{ active() }}</p>Navbar
Links can sit anywhere in the element you pass, including in a bsMenu. When a link in a menu is active, its menu toggle gets .active too.
<nav #navbar class="navbar navbar-expand bg-1 px-3 mb-3 rounded-2" aria-label="Scrollspy navbar">
<span class="navbar-brand">Navbar</span>
<ul class="nav nav-pills">
<li class="nav-item">
<a class="nav-link" href="#spy-navbar-first">First</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#spy-navbar-second">Second</a>
</li>
<li class="nav-item">
<button type="button" class="nav-link" [bsMenuToggle]="more">More</button>
<div bsMenu #more="bsMenu">
<a class="menu-item" href="#spy-navbar-third">Third</a>
<a class="menu-item" href="#spy-navbar-fourth">Fourth</a>
</div>
</li>
</ul>
</nav>
<div
[bsScrollspy]="navbar"
smoothScroll
class="overflow-y-auto position-relative bg-1 p-3 rounded-2"
style="height: 200px"
tabindex="0"
role="region"
aria-label="Navbar scrollspy content">
@for (section of sections; track section) {
<section [id]="`spy-navbar-${section}`">
<h3 class="h5 text-capitalize">{{ section }} heading</h3>
<p>
This is some placeholder content for the scrollspy page. As you scroll, the link for this
heading is highlighted. Links in a menu work too, and the menu's toggle is highlighted with
them.
</p>
</section>
}
</div>Nested nav
Scrollspy also works with nested navigation. When a nested link is active, the link just before its nav is active too.
Item 1
This is some placeholder content for the scrollspy page. When a nested link is active, the link above it is active too.
Item 1-1
This is some placeholder content for the scrollspy page. When a nested link is active, the link above it is active too.
Item 1-2
This is some placeholder content for the scrollspy page. When a nested link is active, the link above it is active too.
Item 2
This is some placeholder content for the scrollspy page. When a nested link is active, the link above it is active too.
Item 3
This is some placeholder content for the scrollspy page. When a nested link is active, the link above it is active too.
Item 3-1
This is some placeholder content for the scrollspy page. When a nested link is active, the link above it is active too.
Item 3-2
This is some placeholder content for the scrollspy page. When a nested link is active, the link above it is active too.
<div class="row">
<div class="col-4">
<nav #nestedNav class="h-100 pe-3 border-end" aria-label="Nested sections">
<div bsNav variant="pills" vertical>
<a class="nav-link" href="#spy-nested-1">Item 1</a>
<div bsNav variant="pills" vertical>
<a class="nav-link ms-3 my-1" href="#spy-nested-1-1">Item 1-1</a>
<a class="nav-link ms-3 my-1" href="#spy-nested-1-2">Item 1-2</a>
</div>
<a class="nav-link" href="#spy-nested-2">Item 2</a>
<a class="nav-link" href="#spy-nested-3">Item 3</a>
<div bsNav variant="pills" vertical>
<a class="nav-link ms-3 my-1" href="#spy-nested-3-1">Item 3-1</a>
<a class="nav-link ms-3 my-1" href="#spy-nested-3-2">Item 3-2</a>
</div>
</div>
</nav>
</div>
<div class="col-8">
<div
[bsScrollspy]="nestedNav"
class="overflow-y-auto position-relative"
style="height: 280px"
tabindex="0"
role="region"
aria-label="Nested scrollspy content">
@for (item of items; track item) {
<section [id]="`spy-nested-${item}`">
@if (item.includes('-')) {
<h4 class="h6">Item {{ item }}</h4>
} @else {
<h3 class="h5">Item {{ item }}</h3>
}
<p>
This is some placeholder content for the scrollspy page. When a nested link is active,
the link above it is active too.
</p>
</section>
}
</div>
</div>
</div>List group
Scrollspy works with list groups as well.
Item 1
This is some placeholder content for the scrollspy page. As you scroll, the list group item for this heading is highlighted.
Item 2
This is some placeholder content for the scrollspy page. As you scroll, the list group item for this heading is highlighted.
Item 3
This is some placeholder content for the scrollspy page. As you scroll, the list group item for this heading is highlighted.
Item 4
This is some placeholder content for the scrollspy page. As you scroll, the list group item for this heading is highlighted.
<div class="row">
<div class="col-4">
<div bsListGroup #list aria-label="List group sections">
@for (item of items; track item) {
<a bsListGroupItem [href]="`#spy-list-${item}`">Item {{ item }}</a>
}
</div>
</div>
<div class="col-8">
<div
[bsScrollspy]="list"
class="overflow-y-auto position-relative"
style="height: 200px"
tabindex="0"
role="region"
aria-label="List group scrollspy content">
@for (item of items; track item) {
<section [id]="`spy-list-${item}`">
<h3 class="h5">Item {{ item }}</h3>
<p>
This is some placeholder content for the scrollspy page. As you scroll, the list group
item for this heading is highlighted.
</p>
</section>
}
</div>
</div>
</div>API
BsScrollspy
Selector: [bsScrollspy] · Exported as: bsScrollspy
Inputs
| Name | Type | Default | Description |
|---|---|---|---|
bsScrollspy | HTMLElement | required | The element holding the links. |
topMargin | string | '12%' | Position of the activation line, as a percentage or in pixels. |
smoothScroll | boolean | false | Scrolls smoothly to a section when its link is clicked. |
Outputs
| Name | Type | Description |
|---|---|---|
activate | string | Emits the id of each section that becomes active. |
Methods
| Name | Type | Description |
|---|---|---|
refresh() | void | Finds the links and sections again. |
activeId | Signal<string | null> | Id of the active section. |