Professional Documents
Culture Documents
Scrollspy Bootstrap v5.0
Scrollspy Bootstrap v5.0
View on GitHub
Scrollspy
Automatically update Bootstrap navigation or list group components
based on scroll position to indicate which link is currently active in the
viewport.
On this page
How it works
Example in navbar
Example with nested nav
Example with list-group
Usage
Via data attributes
Via JavaScript
Methods
refresh
dispose
getInstance
Options
Events
How it works
Scrollspy has a few requirements to function properly:
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.
three
Placeholder content for the scrollspy example. If you wanna dance, if you want it all, you know that
I'm the girl that you should call. Walk through the storm I would. So let me get you in your birthday
suit. The one that got away. Last Friday night, yeah I think we broke the law, always say we're gonna
stop. 'Cause she's a little bit of Yoko, And she's a little bit of 'Oh no'. I want the jaw droppin', eye
poppin', head turnin', body shockin'. Yeah, we maxed our credit cards and got kicked out of the bar.
Item 3-2
Item 1-2
Placeholder content for the scrollspy example. This one relates
Item 2 to item 3-2. You're original, cannot be replaced. All night they're
playing, your song. California girls we're undeniable. Like a bird
Item 3 without a cage. There is no fear now, let go and just be free, I will
love you unconditionally. I can see the writing on the wall. You
Item 3-1
could travel the world but nothing comes close to the golden
coast.
Item 3-2
Item 2
Item 4
Placeholder content for the scrollspy list-group example. This
Item 3 one relates to item 4. Summer after high school when we first
met. There is no fear now, let go and just be free, I will love you
Item 4
unconditionally. Sun-kissed skin so hot we'll melt your popsicle.
This love will make you levitate.
Usage
Via JavaScript
After adding position: relative; in your CSS, call the scrollspy via JavaScript:
Methods
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:
dispose
Destroys an element’s scrollspy. (Removes stored data on the DOM element)
getInstance
Static method which allows you to get the scrollspy instance associated with a DOM element
Options
Options can be passed via data attributes or JavaScript. For data attributes, append the option name
to data-bs-, as in data-bs-offset="".
offset number 10 Pixels to offset from top when calculating position of scroll.
method string auto Finds which section the spied element is in. auto will choose the
best method to get scroll coordinates. offset will use the
Element.getBoundingClientRect() method to get scroll
coordinates. position will use the HTMLElement.offsetTop and
HTMLElement.offsetLeft properties to get scroll coordinates.
activate.bs.scrollspy This event fires on the scroll element whenever a new item becomes activated by
the scrollspy.
Designed and built with all the love in the world by the Bootstrap team with the help of our contributors.
Currently v5.0.0-beta3. Code licensed MIT, docs CC BY 3.0.