Scroll Trigger
Tracks section visibility and syncs navigation state. The sticky menu on this page is running it.
v0.1.2@magic-spells/scroll-triggerPowering this page
Usage
import ScrollTrigger from '@magic-spells/scroll-trigger';
const trigger = new ScrollTrigger({
sections: '.collection-section',
offset: 100,
onIndexChange: ({ currentIndex, currentElement }) => {
// Update your navigation
console.log('Active section:', currentIndex);
console.log('Trigger element:', currentElement);
}
});About
Lightweight scroll-spy plugin for tracking section visibility and syncing navigation state. Perfect for collection pages, documentation, and long-form content. Only 1.5kb gzipped.
Features
- 🪶 Tiny bundle - Only 1.5kb gzipped
- 🎯 IntersectionObserver-based - Modern, performant section tracking
- 🔄 Callback system - Easy integration with custom navigation
- ⚡ Throttled updates - Optimized performance with configurable throttling
- 📍 Precise control - Customizable trigger offset from viewport bottom
- 🎨 Zero dependencies - Pure vanilla JavaScript
Configuration Options
| Option | Type | Default | Description |
|---|---|---|---|
| sections | string|NodeList|Array | required | Sections to track (CSS selector, NodeList, or Array) |
| offset | number|string | 100 | Distance from bottom of viewport to trigger active state (px or percentage like '50%') |
| threshold | number | 0.1 | IntersectionObserver threshold (0-1) |
| throttle | number | 100 | Throttle delay for updates (ms) |
| behavior | string | 'smooth' | Scroll behavior ('smooth' or 'auto') |
| onIndexChange | function | null | Callback when active section changes (receives object: { currentIndex, previousIndex, currentElement, previousElement }) |
About, features, usage and options are taken from the package README.