Class ScrollProgress<CallbacksMap, StaticProps, MutableProps>

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

Type Parameters

Hierarchy (View Summary)

Constructors

Properties

_callbacks: Callbacks<CallbacksMap>

Callbacks instance

_destroyable: () => void[] = []

Stores actions that need to be executed when the module is destroyed

_isDestroyed: boolean = false

Tracks whether the module has been destroyed

_isVisible: boolean = false

Indicates whether the section is currently visible within the viewport or root element.

_props: TTRequiredProps<MutableProps & StaticProps>

Current properties

_rootBounds: IScrollProgressBounds = ...

The bounds of the root element used for scroll calculations.

_sectionBounds: IScrollProgressBounds = ...

The bounds of the section element relative to the root element.

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

  • 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