Searching Open Sourcery

Type to search Open Sourcery.

Motion & Scroll

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

$ npm i @magic-spells/scroll-trigger

GitHubnpm

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

OptionTypeDefaultDescription
sectionsstring|NodeList|ArrayrequiredSections to track (CSS selector, NodeList, or Array)
offsetnumber|string100Distance from bottom of viewport to trigger active state (px or percentage like '50%')
thresholdnumber0.1IntersectionObserver threshold (0-1)
throttlenumber100Throttle delay for updates (ms)
behaviorstring'smooth'Scroll behavior ('smooth' or 'auto')
onIndexChangefunctionnullCallback when active section changes (receives object: { currentIndex, previousIndex, currentElement, previousElement })

About, features, usage and options are taken from the package README.