simpleParallax.js Documentation

repository·master·Indexed 24 days ago

https://github.com/geosigno/simpleparallax.js

A lightweight library for adding parallax effects to <img> and <video> tags, supporting React, Next.js, and vanilla JavaScript. Version 7.0.0 includes a SimpleParallax React component, the useParallaxTransform hook, and a vanilla JavaScript class. It features built-in TypeScript definitions, support for various movement orientations, and automatic respect for the prefers-reduced-motion media query.

Tokens
2.1K
Snippets
5
Records
11
Agent score
79%

What's inside simpleparallax.js

  1. Install simpleParallax.js

    master

    Install the package using your preferred package manager.

    Note for React users: react and react-dom (>= 17) are peer dependencies. The package includes built-in TypeScript definitions for both React and Vanilla entry points.

    npm install simple-parallax-js
    yarn add simple-parallax-js
    pnpm add simple-parallax-js
    bun add simple-parallax-js
  2. Configure ParallaxOptions

    master

    The ParallaxOptions interface defines the configuration settings for a parallax effect. Use these properties to control the movement direction, speed, and visual behavior of the parallax elements.

    Available options:

    • delay: A number representing the delay before the effect starts.
    • maxTransition: A number defining the maximum transition distance.
    • orientation: An Orientation value determining the direction of movement.
    • overflow: A boolean indicating whether to handle overflow behavior.
    • scale: A number controlling the scaling factor.
    • transition: A string defining the CSS transition timing function.
    export interface ParallaxOptions {
      delay: number;
      maxTransition: number;
      orientation: Orientation;
      overflow: boolean;
      scale: number;
      transition: string;
    }
  3. Configure SimpleParallax settings

    master

    When instantiating SimpleParallax, you can provide an optional options object to customize the behavior of the parallax instances. These options are merged with the library's DEFAULTS.

    Common configuration keys include:

    • customContainer: An element that acts as the viewport/container for the parallax calculations. If provided, visibility is checked relative to this container.
    • overflow: A boolean that determines if the element should be wrapped in a container. If set to false, the library will unwrap the element when destroy() is called.
    • scale: A number determining the scaling factor of the parallax effect.
    • orientation: A string determining the direction of the effect.

    Note: The settings object is applied to all elements passed to the constructor.

  4. Default configuration options for simpleParallax.js

    master

    When initializing a parallax effect without providing specific settings, the library uses the following default values. These options control the timing, direction, scaling, and CSS transition behavior of the parallax movement.

    {
      delay: 0.4,
      orientation: "up",
      scale: 1.5,
      overflow: false,
      transition: "cubic-bezier(0,0,0,1)",
      maxTransition: 0
    }
  5. Configure SimpleParallaxProps for React

    master

    When using simple-parallax-js in a React environment, the SimpleParallaxProps interface defines the configuration options available for the parallax component. These props control the movement behavior, transition timing, and visual constraints of the parallax effect.

    export interface SimpleParallaxProps {
      children?: React.ReactNode;
      delay?: number;
      maxTransition?: number | null;
      orientation?: Orientation;
      overflow?: boolean;
      scale?: number;
      transition?: string;
    }
  6. Refresh parallax calculations

    master
    If the layout changes in a way that isn't caught by the window resize event (for example, dynamic content injection or manual DOM manipulation), you can call the .refresh() method. This forces the library to re-calculate viewport positions and update the element offsets and ranges for all managed instances.
  7. Initialize SimpleParallax for vanilla JavaScript

    master

    To create parallax effects on elements using vanilla JavaScript, instantiate the SimpleParallax class. You can pass a single element, a string (selector), a NodeList, an HTMLCollection, or an array of Element objects.

    By default, the library respects the user's system preference for reduced motion. If prefers-reduced-motion is enabled, the parallax effects will not be initialized. The library also listens for changes to this preference dynamically: if a user enables reduced motion, the effects are destroyed; if they disable it, the effects are reinitialized.

    Supported elements are automatically converted to an array internally.

  8. Destroy SimpleParallax instances

    master

    To clean up parallax effects, remove event listeners (like resize), stop animation frames, and reset element styles, call the .destroy() method on your SimpleParallax instance.

    If you configured overflow: false in your settings, calling destroy() will also unwrap the elements from their parallax wrapper containers. If no more active instances exist in the library's internal registry, the global animation loop and resize listeners are fully detached.

  9. Use the SimpleParallax React component

    master

    The SimpleParallax component is a React wrapper that applies parallax effects to its children (typically an <img> element). It automatically handles intersection observation and image height detection to apply transformations.

    To use it, wrap an image element (or any element with a src prop) with <SimpleParallax>. The component clones the child and injects a ref to manage the parallax transformation.

    Props

    PropTypeDefaultDescription
    delaynumberDEFAULTS.delayDelay in milliseconds before the parallax effect starts.
    orientationstringDEFAULTS.orientationThe direction of the parallax effect.
    scalenumberDEFAULTS.scaleThe scale factor applied to the image.
    overflowbooleanDEFAULTS.overflowWhether the container should have overflow: visible or hidden.
    transitionstringDEFAULTS.transitionThe CSS transition property/timing used for the effect.
    maxTransitionnumber0The maximum transition value.
  10. Set the parallax orientation

    master

    The Orientation type specifies the direction in which the parallax effect moves. You can choose from cardinal directions, diagonal directions, or single axes.

    Supported values:

    • "up"
    • "right"
    • "down"
    • "left"
    • "up left"
    • "up right"
    • "down left"
    • "down right"
    export type Orientation =
      | "up"
      | "right"
      | "down"
      | "left"
      | "up left"
      | "up right"
      | "down left"
      | "down right";
  11. Use the useParallaxTransform hook

    master

    The useParallaxTransform hook is a React hook designed to apply parallax transformations to an image element. It manages the calculation of translation values based on scroll progress and integrates with a shared AnimationManager to ensure efficient updates via RequestAnimationFrame (RAF).

    Key Behaviors

    • Performance: It uses a shared animation loop and updates the element's style.transform directly to avoid React re-renders during scroll.
    • Accessibility: It automatically respects the prefers-reduced-motion media query via the useReducedMotion hook, disabling parallax if the user has requested reduced motion.
    • Lifecycle:
      • It registers the element with the AnimationManager only when isVisible is true.
      • It applies an initial scale once the image is isLoaded.
      • It applies a CSS transition after the initial placement to prevent visual jumps.

    Props

    PropTypeDescription
    delaynumberDelay for the CSS transition.
    imageHeightnumberThe height of the image being parallaxed.
    imageRefReact.RefObject<HTMLImageElement>A ref pointing to the image element to be transformed.
    isLoadedbooleanIndicates if the image has finished loading.
    isVisiblebooleanIndicates if the element is currently within the viewport.
    maxTransitionnumberThe maximum parallax intensity/transition value.
    orientationOrientationThe axis of movement (e.g., vertical or horizontal).
    overflowbooleanWhether to handle overflow settings for the transform.
    scalenumberThe scale factor applied to the image.
    transitionstringThe CSS transition timing function (e.g., ease-out).