jscottsmith / react-scroll-parallax

🔮 React hooks and components to create parallax scroll effects for banners, images or any other DOM elements.
https://react-scroll-parallax.damnthat.tv/
MIT License
2.9k stars 158 forks source link

Is it possible to animate custom properties? #241

Closed niklasp closed 8 months ago

niklasp commented 8 months ago

I want to animate the textstroke of an element

-webkit-text-stroke: 
-text-stroke: 

or css variables on scroll. is that possible? how can i achieve it with some hack? e.g. get the progress of the parallax and then set it manually? any help appreciated

jscottsmith commented 8 months ago

Of course, not really a hack. Set a variable on progress like so:

function Example() {
  const parallax = useParallax({
    onProgressChange: (progress) => {
      if (parallax.ref.current) {
        // set progress to CSS variable
        parallax.ref.current.style.setProperty(
          "--progress",
          progress.toString()
        );
      }
    },
  });
  return (
    <h1
      ref={parallax.ref}
      className="thing"
      // use the progress to change the width of the border
      style={{ WebkitTextStrokeWidth: `calc(20px * var(--progress))` }}
    >
      HELLO
    </h1>
  );
}

Here's a CodeSandbox to see it:

https://codesandbox.io/p/sandbox/react-scroll-parallax-text-stroke-klwy4l

Hope that helps.