Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Bootstrap4 Scrollspy

Bootstrap4 Scrollspy

The Scrollspy plugin, i.e., the auto-updating navigation plugin, automatically updates the corresponding navigation target based on the scrollbar position. Its basic implementation is as you scroll.


How to Create Scrollspy

The following example demonstrates how to create scrollspy:

Example

<body data-spy="scroll" data-target=".navbar" data-offset="50">
<nav class=">

navbar navbar-expand-sm bg-dark navbar-dark fixed-top

"

...

<ul class="navbar-nav">
<li>
<a href="#section1">

Section 1

a

li

...

nav

<div id="section1">
<h1>

Section 1

h1

<

p

Try to scroll this page and look at the navigation bar while scrolling!

p

div

...

body

Try it »

Example Parsing

Add data-spy="scroll" to the element you want to spy on (usually the body).

Then add the data-target attribute, whose value is the navbar's id or class (.navbar). This links the scrollable area.

Note that the id on the scrollable item element (