Bootstrap 4 Scrollspy
The Scrollspy plugin, that is, the auto-update 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
Try it »
Example Analysis
Add to the element you want to monitor (usually body)data-spy="scroll" 。
Then adddata-targetan attribute, whose value is the navbar's id or class (.navbar). This makes it connect to the scrollable area.
Note that the id on the scrollable item element (<div id="section1">) must match the link options on the navbar (<a href="../index.html">)。
Optionaldata-offsetattribute is used to calculate the offset pixels from the top when computing the scroll position. The default is 10 px.
Set relative positioning:The element using data-spy="scroll" needs to have its CSSpositionproperty set to "relative" to work.
The following example sets up vertical scrollspy:
Example
Try it »