Locomotive Scroll

repository·main·Indexed 27 days ago

https://github.com/locomotivemtl/locomotive-scroll

A lightweight, TypeScript-first modern scroll library built on top of Lenis for detection, animation, and smooth scrolling. It provides a variety of data-scroll attributes for parallax effects, viewport detection, trigger offsets, and progress tracking, as well as methods to control scroll playback and instance lifecycle.

Tokens
16.6K
Snippets
67
Records
97
Agent score
92%

What's inside locomotive-scroll

  1. Overview of Locomotive Scroll v5

    main

    Locomotive Scroll is a lightweight JavaScript library (9.4kB gzipped) designed for smooth scrolling animations and advanced scroll interactions. It is built on top of Lenis and focuses on performance and accessibility.

    Key capabilities include:

    • Viewport Detection: Uses the browser's native Intersection Observer API.
    • Parallax Effects: Implement parallax using the data-scroll-speed attribute.
    • Progress Tracking: Access real-time scroll progress (0-1) via CSS variables or JavaScript events.
    • Custom Events: Trigger callbacks when elements enter or leave the viewport.
    • Accessibility: Supports native scrollbars, keyboard navigation, and proper ARIA support.
    • Performance: Elements only subscribe to Request Animation Frame (RAF) when they are both visible and animating.
  2. Prevent scroll smoothing for dynamically injected popups

    main

    When third-party scripts inject popups or modals that require independent scrolling, they may conflict with the default scroll wrapper. While adding data-lenis-prevent to the DOM element is a standard solution, it fails if the element is injected dynamically.

    To resolve this for dynamic elements, use the prevent option within lenisOptions. This option accepts a function that receives the DOM node; if the function returns true, scroll smoothing is prevented for that element.

    import LocomotiveScroll from 'locomotive-scroll';
    
    const locomotiveScroll = new LocomotiveScroll({
        lenisOptions: {
            prevent: (node) => node.getAttribute('id') === 'modalSelector',
        },
    });
  3. Include Locomotive Scroll essential styles

    main

    Locomotive Scroll requires base CSS to function correctly. You can import the CSS file into your stylesheet or link to the CDN version in your HTML.

    /* Via CSS import */
    @import 'locomotive-scroll/dist/locomotive-scroll.css';
    <!-- Via HTML link -->
    <link
        rel="stylesheet"
        href="https://cdn.jsdelivr.net/npm/locomotive-scroll/bundled/locomotive-scroll.css"
    />
  4. Deploy the documentation website

    main

    You can deploy the website using different methods depending on your hosting configuration:

    Using SSH: Set the USE_SSH environment variable to true.

    Using GitHub Pages (Non-SSH): Provide your GitHub username via the GIT_USER environment variable. This will build the site and push it to the gh-pages branch.