Tailwind CSS Scroll Nav Plugin API
Headless, unstyled scroll nav plugin built with JavaScript and TypeScript, including installation, usage, options, methods, events, and selectors.
Installation
To get started, install Scroll Nav plugin via npm, else you can skip this step if you are already using Preline UI as a package.
-
Install the plugin
Install
@preline/scroll-navvia npmTerminalnpm i @preline/scroll-nav -
Add the plugin CSS
Use @source to register the plugin's JavaScript path for Tailwind CSS scanning, then @import the plugin's CSS files into your Tailwind CSS file.
main.css@import "tailwindcss"; /* @preline/scroll-nav */ /* [!code highlight:3] */ @source "../node_modules/@preline/scroll-nav/*.js"; @import "./node_modules/@preline/scroll-nav/variants.css"; @import "./node_modules/@preline/scroll-nav/theme.css"; -
Add the plugin JavaScript
Include the JavaScript
<script>that powers the interactive elements near the end of your</body>tag:HTML
Additional Initialization Options
Use the
non-autoentry if you need manual initialization. In this mode, automatic initialization on page load is not included, so the component should be initialized explicitly.HTML (non-auto)
Via bundler
When using a bundler (Vite, webpack, etc.), import the plugin directly as an ES module.
JavaScript (auto)import "@preline/scroll-nav";JavaScript (non-auto)import HSScrollNav from "@preline/scroll-nav/non-auto"; HSScrollNav.autoInit(); // Or initialize a specific element manually const el = document.querySelector("#scroll-nav"); if (el) new HSScrollNav(el);
Example
A horizontally scrolling navigation component that moves seamlessly within a carousel.
Basic usage
Prefer to create your own style? Here is a completely unstylized example.
<div data-hs-scroll-nav class="relative px-6">
<button type="button" class="hs-scroll-nav-prev">Previous</button>
<nav class="hs-scroll-nav-body flex flex-nowrap overflow-x-auto snap-x snap-mandatory">
<a class="snap-start" href="#">Electronics</a>
<a class="snap-start" href="#">Clothing</a>
<a class="snap-start" href="#">House & Garden</a>
<a class="snap-start" href="#">Baby Care</a>
<a class="snap-start" href="#">Home appliances</a>
<a class="snap-start" href="#">Sports & Fitness</a>
<a class="snap-start" href="#">Constraction & Renovation</a>
<a class="snap-start" href="#">Food</a>
<a class="snap-start" href="#">Pharmacy</a>
<a class="snap-start" href="#">Pet Products</a>
<a class="snap-start" href="#">Books</a>
<a class="snap-start" href="#">Tourism, fishing, hunting</a>
<a class="snap-start" href="#">Furniture</a>
<a class="snap-start" href="#">Jewelery</a>
<a class="snap-start" href="#">Accessories</a>
<a class="snap-start" href="#">Games & Consoles</a>
<a class="snap-start" href="#">Stationery</a>
<a class="snap-start" href="#">Digital Goods</a>
<a class="snap-start" href="#">Music</a>
</nav>
<button type="button" class="hs-scroll-nav-next">Next</button>
</div>
Methods
The HSScrollNav object is contained within the global window object
Destroy instance.
const { element } = HSScrollNav.getInstance('#scroll-nav', true);
const ecommerce = document.querySelector('#ecommerce-link');
element.goTo(ecommerce, () => {
console.log('Scrolled!');
});
Ready to use Components
Looking for prebuilt UI components based on the Tailwind CSS? Preline UI packs hundreds of component examples for all your website needs.
Scroll Nav
Explore ready-to-use Tailwind CSS examples built with Preline UI.
View Scroll Nav examples