scrollama

repository·main·Indexed 27 days ago

https://github.com/russellsamora/scrollama

A lightweight JavaScript library for creating scrollytelling experiences using the IntersectionObserver API. Version 3.2.0 allows developers to trigger animations or content changes as elements enter or exit the viewport via callbacks like onStepEnter, onStepExit, and onStepProgress.

Tokens
1.6K
Snippets
2
Records
10
Agent score
41%

What's inside scrollama

  1. Install Scrollama

    main

    You can install Scrollama via npm or include it via a script tag.

    Important: As of version 1.4.0, the IntersectionObserver polyfill is not included. You must manually add a polyfill for cross-browser support if you need to support older browsers.

    NPM Installation:

    npm install scrollama --save

    Importing:

    import scrollama from "scrollama";
    // or
    const scrollama = require("scrollama");

    CDN/Global (Old school):

    <script src="https://unpkg.com/scrollama"></script>
    npm install scrollama --save
  2. Basic Usage with Step Triggers

    main

    To use Scrollama for basic step triggering, define your step elements in HTML and then instantiate the scroller in JavaScript.

    HTML Structure:

    <div class="step" data-step="a"></div>
    <div class="step" data-step="b"></div>
    <div class="step" data-step="c"></div>

    JavaScript Setup:

    const scroller = scrollama();
    
    scroller
      .setup({
        step: ".step",
      })
      .onStepEnter((response) => {
        // response: { element, index, direction }
      })
      .onStepExit((response) => {
        // response: { element, index, direction }
      });
    const scroller = scrollama();
    
    scroller
      .setup({
        step: ".step",
      })
      .onStepEnter((response) => {
        // { element, index, direction }
      })
      .onStepExit((response) => {
        // { element, index, direction }
      });
  3. Initialize Scrollama with setup()

    main

    To use Scrollama, call the scrollama() function to get a controller object, then call .setup() to initialize it with your step elements and configuration.

    Configuration Options:

    • step: A selector string used to find the elements that will act as scroll steps.
    • parent: The parent element context for the step selector.
    • offset (default: 0.5): The scroll offset at which a step is triggered. Can be a number or a string parsed via parseOffset (e.g., from data-offset).
    • threshold (default: 4): Used when progress is enabled to define the granularity of progress updates.
    • progress (default: false): Boolean to enable progress tracking for steps.
    • once (default: false): If true, each step will only trigger its stepEnter callback once.
    • debug (default: false): Enables debug logging.
    • container: The element that acts as the scroll container.
    • root: The root element for the IntersectionObserver (defaults to null, which is the viewport).
  4. Control the Scrollama instance

    main

    Use these methods to manage the lifecycle and behavior of your scroller:

    • offsetTrigger([number | string]): Get or set the offset percentage. Value must be between 0-1 or a string with "px" (e.g., "200px"). Returns the instance.
    • resize(): Tells Scrollama to get latest dimensions. Note: Built-in resize observers make this mostly unnecessary, but it is useful if you manually update DOM elements.
    • enable(): Resumes observing for trigger changes.
    • disable(): Stops observing for trigger changes.
    • destroy(): Removes all observers and callback functions.
  5. Configure scrollama.setup() options

    main

    The .setup([options]) method initializes the scroller.

    OptionTypeDescriptionDefault
    stepstring or HTMLElement[]required Selector (or array of elements) for the step elements.-
    offsetnumber (0-1) or string (e.g. "px")How far from the top of the viewport to trigger a step.0.5
    progressbooleanWhether to fire incremental step progress updates.false
    thresholdnumber (1 or higher)The granularity of the progress interval in pixels (smaller = more granular).4
    oncebooleanOnly trigger the step to enter once then remove listener.false
    debugbooleanWhether to show visual debugging tools.false
    parentHTMLElement[]Parent element for step selector (use if steps are in shadow DOM).undefined
    containerHTMLElementParent element for the scroll story (use if nested in an element with overflow: scroll or auto).undefined
    rootHTMLElementThe element used as the viewport for checking visibility. Defaults to browser viewport.undefined
  6. Handle Step Enter, Exit, and Progress events

    main

    Scrollama provides three main callback methods to respond to scroll position:

    1. onStepEnter(callback): Fires when the top or bottom edge of a step enters the offset threshold.

      • Callback argument: { element: DOMElement, index: number, direction: string }
      • direction is 'up' or 'down'.
    2. onStepExit(callback): Fires when the top or bottom edge of a step exits the offset threshold.

      • Callback argument: { element: DOMElement, index: number, direction: string }
    3. onStepProgress(callback): Fires incremental progress updates if progress: true is set in .setup().

      • Callback argument: { element: DOMElement, index: number, progress: number, direction: string }
      • progress is the percent of completion (0 - 1).
  7. Set custom offsets via data attributes

    main

    You can override the global offset option for individual elements by adding a data-offset attribute directly to the step element in your HTML. This supports both decimal values (0-1) and pixel strings.

    Example:

    <div class="step" data-offset="0.25"></div>
    <div class="step" data-offset="100px"></div>
  8. Handle step events with onStepEnter, onStepExit, and onStepProgress

    main

    Register callbacks to respond to scroll events. The callbacks receive a response object containing the element, the index of the step, and the scroll direction ('down' or 'up'). If progress is enabled, onStepProgress also receives a progress value (0 to 1).

    Callback Response Object:

    • element: The DOM node of the step.
    • index: The index of the step in the sequence.
    • direction: 'down' or 'up'.
    • progress: (Only in onStepProgress) A value between 0 and 1.
  9. Control Scrollama lifecycle: enable, disable, destroy, and resize

    main

    Use these methods to manage the active state of the scroller:

    • enable(): Activates the observers to start tracking scroll position.
    • disable(): Disconnects observers to stop tracking.
    • destroy(): Disconnects observers and resets all internal callbacks and state.
    • resize(): Manually triggers an update to the observers (useful if the layout changes significantly).