svelte-scrollactive
About
This is a port from vue-scrollactive to Svelte.
You can check the demo on REPL and follow the common configuration for the module (check the example code on github repo).
Remember to check if you're on the browser if you're using SvelteKit.
Main differences from the Vue component:
- The default scroll active class is 'active' instead of 'is-active'
- You can use it without direct children (see below for example and why would you use this way).
- There is an exported function called
scrollToElement
so that you can scroll as you wish.
This component makes it simple to highlight a menu item with an 'active' class as you scroll.
- Highlights items with a class as you scroll
- Scrolls to item's section on click
- Configurable easing scroll effect
- Emits events for full control
Make sure to check the REPL demo where you can play around with every option.
Installation
Install using pnpm
pnpm add svelte-scrollactive
or yarn
yarn add svelte-scrollactive
or npm
npm install --save svelte-scrollactive
then import the plugin
import { Scrollactive } from 'svelte-scrollactive';
Primary Usage
The primary way to use the plugin is to wrap your menu in a <Scrollactive>
tag (which will be your nav) and add a .scrollactive-item
class in your <a>
tags as I show in the example below:
<Scrollactive class="my-nav">
<a href="#home" class="scrollactive-item">Home</a>
<a href="#about-us" class="scrollactive-item">About Us</a>
<a href="#portfolio" class="scrollactive-item">Portfolio</a>
<a href="#contact" class="scrollactive-item">Contact</a>
</Scrollactive>
You can follow whatever structure you wish, just make sure to set the .scrollactive-item
class in the items you want to highlight and set its href
with a valid element ID that you would like to track while scrolling.
The secondary way to use it is almost the same as the primary but instead of relying on href
to find your sections you'll need to set a data attribute data-section-selector
on your elements with the section selector you wish to have.
<Scrollactive class="my-nav">
<span data-section-selector="#home" class="scrollactive-item">Home</span>
<span data-section-selector=".about-us" class="scrollactive-item">About Us</span>
<span data-section-selector=".portfolio div span" class="scrollactive-item">Portfolio</span>
<span data-section-selector="#contact" class="scrollactive-item">Contact</span>
</Scrollactive>
As you can see this gives you more freedom to choose different tags and you can use whatever CSS selector you find necessary, but it's important to notice that data-section-selector
takes precedence over href
, so if you have a tag <a href="#section-1" data-section-selector="#another-section">
it will completely ignore the #section-1
and use #another-section
instead.
SvelteKit Usage
You can use the first way with Sveltekit but checking if you're in the browser first. However, this will take long to show your content. So a better way is to load your content from server first (from load function) and add ScrollActive side by side like so:
import { Scrollactive } from 'svelte-scrollactive';
import { browser } from '$app/environment';
import { navigating } from '$app/stores';
{#if browser}
{#key $navigating}
<Scrollactive offset={120} />
{/key}
{/if}
<slot />
Be sure to use the nav elements like mentioned above (using the class scrollactive-item
). But now they can be anywhere in the page.
You can also use navigating
store with a key
directive to always update scrollactive when the page changes (and content is updated).
Events
Scrollactive will emit an itemchanged(event, currentItem, lastActiveItem)
event when an active menu item is changed to another. You can catch that event doing as the example below:
<Scrollactive class="my-nav" on:itemchanged={onItemChanged}>
<a href="#home" class="scrollactive-item">Home</a>
<a href="#about-us" class="scrollactive-item">About Us</a>
<a href="#portfolio" class="scrollactive-item">Portfolio</a>
<a href="#contact" class="scrollactive-item">Contact</a>
</Scrollactive>
function onItemChanged({event, currentItem, lastActiveItem}) {
}
Configuration
All options should be passed as a prop in the <Scrollactive>
component as you can see in the example below:
<Scrollactive activeClass="is-active" offset="80" duration="800" bezierEasingValue=".5,0,.35,1" />
Remember that all options are optional and you can check the default values below:
Options
activeClass: {
type: String,
default: 'active',
},
offset: {
type: Number,
default: 20,
},
scrollOffset: {
type: Number,
default: null,
},
scrollContainerSelector: {
type: String,
default: '',
},
clickToScroll: {
type: Boolean,
default: true,
},
duration: {
type: Number,
default: 600,
},
alwaysTrack: {
type: Boolean,
default: false,
},
bezierEasingValue: {
type: String,
default: '.5,0,.35,1',
},
modifyUrl: {
type: Boolean,
default: true,
},
exact: {
type: Boolean,
default: false,
},
highlightFirstItem: {
type: Boolean,
default: false,
},
tag: {
type: String,
default: 'nav',
},
scrollOnStart: {
type: Boolean,
default: true,
},
scrollToElement: {
type: Function,
}
Contributing
Clone the repository and install the dependencies running pnpm
. After the dependencies are installed you should be good to run pnpm dev
which will load up a server with the sandbox for you to play around.