Stanko / react-plx

React parallax component, powerful and lightweight
https://muffinman.io/react-plx/
MIT License
689 stars 51 forks source link
animation parallax react scrolling

Plx - React Parallax component

npm version npm downloads

React component, for creating on scroll effects aka. parallax. Lightweight, yet powerful.

Version 2 brings even more performance improvements and TypeScript support.

Changelog

Demo

Check the live demo. You can find source for the demo here.

Plx example, exploding elements

I would really like to see what you people have built using Plx and create a showcase section. So please open an issue titled Showcase: <your awesome stuff> so it can be featured. Cheers!

Quick start

Get it from npm

$ npm install --save react-plx

Import and use it in your React app.

import React, { Component } from "react";
import Plx from "react-plx";

// An array of parallax effects to be applied - see below for detail
const parallaxData = [
  {
    start: 0,
    end: 500,
    properties: [
      {
        startValue: 1,
        endValue: 2,
        property: "scale",
      },
    ],
  },
];

class Example extends Component {
  render() {
    return (
      <Plx className="MyAwesomeParallax" parallaxData={parallaxData}>
        /* Your content */
      </Plx>
    );
  }
}

Table of contents

What is this?

This is React component which makes creating on scroll effects (aka parallax) easy. If you are not sure what it does, demo should help.

It is lightweight, and beside react, react-dom and prop-types has no dependencies, now it has small bezier-easing package. As listening to scroll event is not performant, this component uses different approach. Interval is set (every 16ms to get 60fps) to check if scroll position is changed, and if it is, it broadcasts custom event. All of the Plx components are sharing the scroll manager singleton. Interval is set when the first component is created, and cleared when last one is unmounted. Interval time can be changed through the props, but it is shared across the components.

Elements outside of viewport are not animated. This is done by using getBoundingClientRect, but there is a known bug in iOS with getBoundingClientRect and position fixed. If you get into the same problems, you can force rendering by passing animateWhenNotInViewport={ true }.

Still you need to avoid common "don't dos" when making a parallax page:

Read this great article to find out more (that is where I got my initial inspiration).

Of course, you can break any of these rules, but test for performance to see if it works for you.

Component is written as ES module, so it will work with webpack and other module bundlers (which is standard for React apps anyway). Tested with react-create-app and my boilerplate, Marvin.

Read more about how it works in this blog post.

Props

Any other props will be passed to the component (for example this is useful for aria-* props).

parallaxData

properties

Example of props

These are the exact props used in this example.

const exampleParallaxData = [
  {
    start: 0,
    end: 300,
    properties: [
      {
        startValue: 0,
        endValue: 90,
        property: "rotate",
      },
      {
        startValue: 1,
        endValue: 1.5,
        property: "scale",
      },
      {
        startValue: 1,
        endValue: 0.75,
        property: "opacity",
      },
    ],
  },
  {
    start: 350,
    duration: 300,
    properties: [
      {
        startValue: "#3cb99c",
        endValue: "rgba(50,50,200,0.8)",
        property: "backgroundColor",
      },
      {
        startValue: 0,
        endValue: 100,
        property: "translateY",
      },
      {
        startValue: 0.75,
        endValue: 1,
        property: "opacity",
      },
    ],
  },
  {
    start: 700,
    duration: 1000,
    properties: [
      {
        startValue: 100,
        endValue: 0,
        property: "translateY",
      },
      {
        startValue: 1.5,
        endValue: 2,
        property: "scale",
      },
      {
        startValue: 90,
        endValue: 0,
        property: "rotate",
      },
      // Blur is not performant
      // Used just as an example for CSS filters
      {
        startValue: 0,
        endValue: 20,
        property: "blur",
      },
    ],
  },
];

Animation state CSS classes

Component will also apply CSS classes that match current animation state. Classes are:

active class is applied along with both in and between classes.

Browser support

All modern browsers.

Since version 2, I'm not supporting nor testing it in Internet Explorer. Library will probably work in IE10+ and even IE9 should work if you provide a polyfill for requestAnimationFrame.

License

Released under MIT License.