useScroll

스크롤 애니메이션을 만들때 사용 👉🏻 progress indicators, parallax effects

Element scroll progress

import "./styles.css";
import { useRef } from "react";
import { motion, useScroll } from "framer-motion";

export default function App() {
  const ref = useRef(null); // 스크롤을 감지할 요소에 적용
  const { scrollXProgress } = useScroll({ container: ref });

  return (
    <>
      <svg id="progress" width="100" height="100" viewBox="0 0 100 100">
        <circle cx="50" cy="50" r="30" pathLength="1" className="bg" />
        <motion.circle
          cx="50"
          cy="50"
          r="30"
          pathLength="1"
          className="indicator"
          style={{ pathLength: scrollXProgress }} // X스크롤의 진행률을 svg 요소에 적용
        />
      </svg>
      <ul ref={ref}> // ➡️ 스크롤바가 생기는 요소, X축으로 스크롤바 생김
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
      </ul>
    </>
  );
}

Parallax

https://codesandbox.io/p/sandbox/framer-motion-parallax-i9gwuc?file=%2Fsrc%2FApp.tsx%3A9%2C11&from-embed

Image Component

useParallax

Scroll velocity

  • scrollY 값의 변화 속도를 계산하여 scrollVelocity에 저장합니다. 이 값은 스크롤의 빠르기를 나타내며, 스크롤이 더 빨리 움직일수록 속도 값이 증가

Last updated