- Plugins
- 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.
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>
Methods
The HSScrollspy
object is contained within the global window
object
Destroy instance.
const { element } = HSScrollspy.getInstance('#scrollspy', true);
const destroyBtn = document.querySelector('#destroy-btn');
destroyBtn.addEventListener('click', () => {
element.destroy();
});
Events
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.