我如何在 React 中使用 Rellax.js?

How do I use Rellax.js with React?

我正在尝试将 Rellax.js 与 React 一起使用,但我无法理解如何使用它。我能找到的只有 https://www.npmjs.com/package/rellax#target-node。在那link中,他们给出了这个代码。

<div ref={ref => { this.rellaxRef = ref }}>
  I’m that default chill speed of "-2"
</div>

var rellax = new Rellax(this.rellaxRef)

我如何使用带有 React 钩子的代码以及整个组件的外观。这是一个非常狭窄的组件视图。

你可以像这样使用它 WITH HOOKS:

import React, { useRef, useEffect } from "react";
import Rellax from "rellax";

export default function App() {
  const rellaxRef = useRef();

  useEffect(() => {
    new Rellax(".animate", { // <---- Via class name
      speed: -10,
      center: false,
      wrapper: null,
      round: true,
      vertical: true,
      horizontal: false
    });

    new Rellax(rellaxRef.current, { // <---- Via useRef element
      speed: -10,
      center: false,
      wrapper: null,
      round: true,
      vertical: true,
      horizontal: false
    });
  }, []);

  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
      <div ref={rellaxRef}>
        Lorem Ipsum is simply dummy text of the printing and typesetting
      </div>
      <div className="animate">I’m that default chill speed of "-2"</div>
    </div>
  );
}

工作演示(您可以滚动查看)