Class ScrollProgress

ScrollProgress is a component that tracks the scroll progress of a specified section element.

This component can be used for creating scroll-based animations such as parallax effects.

Documentation

Hierarchy (View Summary)

  • Module<TC, TS, TM>
    • ScrollProgress

Constructors

Accessors

  • get isDestroyed(): boolean

    Checks if the module has been destroyed.

    Returns boolean

  • get name(): string

    The name of the module, derived from the class name

    Returns string

  • get prefix(): string

    Optional prefix for classnames used by the module

    Returns string

Methods

  • Adds a class name on an element, and keeps track of it for removal when the module is destroyed.

    Parameters

    • element: Element

      The target DOM element.

    • className: string

      The class name to toggle.

    Returns void

  • Helper function to generate classnames with the module's prefix.

    Parameters

    • ...classNames: string[]

      The class names to generate.

    Returns string

    A string of class names with the module's prefix applied.

  • Internal method to handle the destruction of the module. It removes all callbacks, destroys properties, and cleans up event listeners and class names.

    Returns void

  • Retrieves the default static properties.

    Returns {
        __staticProp: true;
        optimized: boolean;
        root: Element;
        section: Element;
        useSvh: boolean;
    }

    • __staticProp: true
    • optimized: boolean

      If true, progress is calculated only while the section is within the viewport or the root element. Improves performance by avoiding unnecessary calculations.

      true
      
    • root: Element

      The root element used as a reference for scroll progress calculation. Usually it is the scroll container. If null, the viewport is used as the reference.

      null
      
    • section: Element

      The element whose scroll progress is tracked.

    • useSvh: boolean

      If true, the scroll progress is calculated based on the small viewport height (svh in css) instead of the current viewport height (vh in css).

      false
      
  • Calculates the section scroll progress relative to the root element.

    The function takes top or left corner of the section as the reference point.

    Parameters

    • topThreshold: number

      Top threshold of the section position.

    • rightThreshold: number

      Right threshold of the section position.

    • bottomThreshold: number

      Bottom threshold of the section position.

    • leftThreshold: number

      Left threshold of the section position.

    Returns { x: number; y: number }

    The scroll progress along the x and y axes.

    const progress = getProgress(0, vevet.width, vevet.height / 2, 0)

    // `progress.y` is `0` when the top corner of the section is at the beginning of the viewport or root element
    // `progress.y` is `1` when the top corner of the section is at the center of the viewport or root element
  • Adds a callback on the module's destruction.

    Parameters

    • action: () => void

      The function to execute during destruction.

    Returns void