1. Plugins
  2. Scrollspy

Plugins

Tailwind CSS Scrollspy

Automatically update navigation or list group components based on scroll position to indicate which link is currently active in the viewport.

Scrollspy

Installation

To get started with Scrollspy, install Overlay plugin via npm, else you can skip this step if you are already using Preline UI as a package.

                      
                        npm i @preline/scrollspy
                      
                    

Example

Scroll the area below the navbar and watch the active class change. The dropdown items will be highlighted as well.

First

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

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

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

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

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.

Basic usage

Prefer to create your own style? Here is a completely unstylized example.

                      
                        <div id="hs-scrollspy-scrollable-parent-basic-usage" class="max-h-96 overflow-y-auto">
                          <div data-hs-scrollspy="#hs-scrollspy-basic-usage" class="flex flex-wrap gap-3">
                            <a class="active" href="#first">First</a>
                            <a href="#second">Second</a>
                            <a href="#third">Third</a>
                            <a href="#fourth">Fourth</a>
                            <a href="#fifth">Fifth</a>
                          </div>

                          <div id="hs-scrollspy-basic-usage">
                            <div id="first" class="h-48">
                              First
                            </div>

                            <div id="second" class="h-48">
                              Second
                            </div>

                            <div id="third" class="h-48">
                              Third
                            </div>

                            <div id="fourth" class="h-48">
                              Fourth
                            </div>

                            <div id="fifth" class="h-48">
                              Fifth
                            </div>
                          </div>
                        </div>
                      
                    

Data Options

Name Description Options Default value
data-hs-scrollspy A container containing sections. This must be a valid selector. Should be added to the nav that contains Scrollspy links. string
data-hs-scrollspy-scrollable-parent Specifies the element to be scrolled. This must be a valid selector. Should be added to the nav that contains Scrollspy links. string window

Class Options

Name Description Options Default value
[--scrollspy-offset:*] Adds offset when scrolling to the section and to determine if the section is active. Should be added to the nav that contains Scrollspy links. number 0

Tailwind Modifiers

Name Description
hs-scrollspy-active:* A modifier that allows you to set Tailwind classes when the section is active for link

Methods

The HSScrollspy object is contained within the global window object

Method Description
Public methods
destroy() Destroys the instance, removes generated markup (if any), removes added classes and attributes.
Static methods
HSScrollspy.getInstance(target, isInstance) Returns the element associated to the target.
  • target should be a Node or string (valid selector)
  • isInstance boolean. Returns the instance instead of Node if true

Destroy instance.

                      
                        const { element } = HSScrollspy.getInstance('#scrollspy', true);
                        const destroyBtn = document.querySelector('#destroy-btn');
                        
                        destroyBtn.addEventListener('click', () => {
                          element.destroy();
                        });
                      
                    

Events

Method Description Returning value
on:beforeScroll Called before scrolling begins. Could be Promise. Instance

An example of executing a function after collapsing the accordion.

                      
                        const el = HSScrollspy.getInstance('[data-hs-scrollspy="#scrollspy"]', true);
                        const collapse = HSCollapse.getInstance('[data-hs-collapse="#navbar-collapse-example"]', true);
                
                        el.element.on('beforeScroll', (instance) => {
                          return new Promise((res) => {
                            if (collapse.element.el.classList.contains('open')) {
                              collapse.element.hide();
                              HSStaticMethods.afterTransition(collapse.element.content, () => res(true));
                            } else {
                              res(true);
                            }
                          });
                        });
                      
                    

Demo examples

Looking for prebuilt UI components based on the Tailwind CSS? Preline UI packs hundreds of component examples for all your website needs.

Plugins UI Mockups
Check out Preline UI Components