Framer Motion — Scrolling and Watching Motion Values

John Au-Yeung
3 min readFeb 13, 2021
Photo by John Torcasio on Unsplash

With the Framer Motion library, we can render animations in our React app easily.

In this article, we’ll take a look at how to get started with Framer Motion.

useElementScroll

The useElementScroll hook returns motion values that updated when the element scrolls.

For example, we can write:

import React, { useRef } from "react";
import { motion, useElementScroll } from "framer-motion";
export default function App() {
const ref = useRef();
const { scrollYProgress } = useElementScroll(ref);
return (
<div ref={ref} style={{ overflow: "scroll", height: 200 }}>
<motion.div style={{ scaleY: scrollYProgress }}>
{Array(100)
.fill()
.map((_, i) => (
<p key={i}>{i}</p>
))}
</motion.div>
</div>
);
}

We set the scaleY value to the scrollYProgress motion value.

Then the p elements that are lower are taller.

We just have the overflow property set to 'scroll' and a fixed height for this hook to calculate the scroll progress.

useViewportScroll

--

--

No responses yet