Giter Club home page Giter Club logo

react-just-parallax's Introduction

React Just Parallax

React Just Parallax

React library for scroll and mousemove parallax effect ✨
Open source, production-ready


This repo contains the source code for React Just Parallax, source code for documentation page and source code for testing the package locally.

  • Written in TypeScript
  • Super lightweight
  • Easy to use
  • blazing fast

📦 NPM Link

📜 Examples

⚖️ License

  • React Just Parallax is MIT licensed.

✍🏻 Author

🐇 Quick start

npm install react-just-parallax
import { MouseParallax, ScrollParallax } from "react-just-parallax";

export const MyComponent = () => (
  <>
    <MouseParallax>
      <p>I'm reacting to mouse move</p>
    </MouseParallax>

    <ScrollParallax>
      <p>I'm reacting to scroll</p>
    </ScrollParallax>
  </>
);

Props for MouseParallax

Name Type Default Description
strength number 0.14 Parallax offset multiplier. Moving mouse by 10 pixels will move element position by 10px * strength
lerpEase number 0.06 Determines how quick the interpolation between offset values occurs (the higher the quicker)
isAbsolutelyPositioned boolean false If the element you want to use parallax on is positioned absolutely, set this prop to true
zIndex number | null null Specify element's outer container z-index (useful while using isAbsolutelyPositioned prop)
shouldPause boolean true Stops element from reacting to scroll and interpolating offset if it is out of view
enableOnTouchDevice boolean false Turns on/off parallax effect on touch devices
scrollContainerRef React.MutableRefObject | null null Use when element is situated in scrollable element other than window
parallaxContainerRef React.MutableRefObject | null null By default, element reacts to mousemove on window. You can specify any other container using this prop to make element react only within given container
shouldResetPosition boolean false Resets element's position if cursor leaves window or leaves parallaxContainerRef

Props for ScrollParallax

Name Type Default Description
strength number 0.14 Parallax offset multiplier. Scrolling by 10 pixels will move element position by 10px * strength
lerpEase number 0.06 Determines how quick the interpolation between offset values occurs (the higher the quicker)
isAbsolutelyPositioned boolean false If the element you want to use parallax on is positioned absolutely, set this prop to true
zIndex number | null null Specify element's outer container z-index (useful while using isAbsolutelyPositioned prop)
shouldPause boolean true Stops element from reacting to scroll and interpolating offset if it is out of view
enableOnTouchDevice boolean true Turns on/off parallax effect on touch devices
isHorizontal boolean false Enable if using horizontal scrolling
scrollContainerRef React.MutableRefObject | null null Use when element is situated in scrollable element other than window

Recalculating values on demand for ScrollParallax

It's sometimes necessary to update values such as element's position or sizes on demand, for example if the DOM structure changes.

Library can't know of this kind of changes so it is not able to handle it by itself, and that's when we need to use ScrollParallaxHandle to update them manually.

import { ScrollParallax, ScrollParallaxHandle } from "react-just-parallax";

export const MyComponent = () => {
  const [display, setDisplay] = useState(true);
  const scrollParallaxRef = useRef<ScrollParallaxHandle | null>(null);

  useEffect(() => {
    scrollParallaxRef.current?.updateValues();
  }, [display]);

  return (
    <>
      <ScrollParallax ref={scrollParallaxRef}>
        <p>I'm reacting to scroll</p>
      </ScrollParallax>

      {display && (
        <button onClick={() => setDisplay((false)}>
          Disappear me
        </button>
      )}
    </>
  );
};

react-just-parallax's People

Contributors

michalzalobny avatar drillprop avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.