TIL 4/25

RamiΒ·2025λ…„ 4μ›” 25일

TodayILearn

λͺ©λ‘ 보기
38/61

9.4 Header part Four

animate="scroll" vs animate={navAnimation} 차이

βœ… framer-motion animate="μƒνƒœ" vs animate={useAnimation()} 차이 정리

1. κΈ°λ³Έ κ°œλ…

비ꡐ ν•­λͺ©animate="scroll"animate={navAnimation}
νƒ€μž…λ¬Έμžμ—΄ (string)μ• λ‹ˆλ©”μ΄μ…˜ 컨트둀러 객체
λ™μž‘ λ°©μ‹κ³ μ •λœ μƒνƒœλ§Œ μ‚¬μš©μ‘°κ±΄μ— 따라 μƒνƒœ μ „ν™˜ κ°€λŠ₯
μƒνƒœ λ³€κ²½μžλ™ν”„λ‘œκ·Έλž˜λ°μ μœΌλ‘œ μˆ˜λ™ 쑰절 (start())
μ‚¬μš© μ˜ˆλ‹¨μˆœν•œ μ• λ‹ˆλ©”μ΄μ…˜μŠ€ν¬λ‘€, 클릭 λ“± μ‚¬μš©μž μƒν˜Έμž‘μš©μ— λ”°λ₯Έ 동적 μ œμ–΄

2. μ½”λ“œ 비ꡐ μ˜ˆμ‹œ

πŸ’‘ animate="scroll": λ‹¨μˆœ μ• λ‹ˆλ©”μ΄μ…˜

<motion.div
  variants={{
    initial: { backgroundColor: "blue" },
    scroll: { backgroundColor: "black" },
  }}
  initial="initial"
  animate="scroll"  // κ³ μ •λœ μƒνƒœ
>
  정적인 μ• λ‹ˆλ©”μ΄μ…˜
</motion.div>

πŸ’‘ animate={useAnimation()}: 쑰건 기반 μ• λ‹ˆλ©”μ΄μ…˜

const controls = useAnimation();
const { scrollY } = useViewportScroll();

useEffect(() => {
  scrollY.onChange(() => {
    if (scrollY.get() > 100) {
      controls.start("scrolled");
    } else {
      controls.start("top");
    }
  });
}, [scrollY, controls]);

return (
  <motion.div
    variants={{
      top: { backgroundColor: "blue" },
      scrolled: { backgroundColor: "black" },
    }}
    initial="top"
    animate={controls} // 컨트둀 객체 μ‚¬μš©
  >
    μŠ€ν¬λ‘€μ— 따라 λ™μ μœΌλ‘œ 색 변경됨
  </motion.div>
);

3. πŸ“Œ μ–Έμ œ useAnimation()을 μ“°λ©΄ μ’‹μ„κΉŒ?

상황섀λͺ…
βœ… 슀크둀 μœ„μΉ˜μ— 따라 배경색, μœ„μΉ˜ 등을 λ°”κΎΈκ³  싢을 λ•Œex. 헀더가 슀크둀 μ‹œ 투λͺ… β†’ κ²€μ • 배경으둜 λ°”λ€ŒλŠ” 효과
βœ… 클릭 λ˜λŠ” μ‚¬μš©μž λ™μž‘μœΌλ‘œ μ• λ‹ˆλ©”μ΄μ…˜μ΄ λ°”λ€Œμ–΄μ•Ό ν•  λ•Œex. 검색 λ²„νŠΌ 클릭 μ‹œ input 창이 ν™•μž₯λ˜λŠ” 효과
βœ… μ—¬λŸ¬ μ• λ‹ˆλ©”μ΄μ…˜μ„ λ™μ‹œμ— μ œμ–΄ν•΄μ•Ό ν•  λ•Œex. λ‹€μˆ˜ μ»΄ν¬λ„ŒνŠΈ μƒνƒœλ₯Ό ν•˜λ‚˜μ˜ 쑰건으둜 μ œμ–΄ν•  λ•Œ
βœ… 타이밍을 μ‘°μ ˆν•˜κ±°λ‚˜ μƒνƒœλ₯Ό 연속 μ „ν™˜ν•  λ•Œex. νŠΉμ • 쑰건이 만쑱될 λ•Œλ§ˆλ‹€ .start()둜 흐름 μ œμ–΄

🧠 마무리 정리

  • λ‹¨μˆœνžˆ "λ“€μ–΄μ˜¬ λ•Œ ν•œλ²ˆλ§Œ μ‹€ν–‰"λ˜λŠ” μ• λ‹ˆλ©”μ΄μ…˜μ΄λ©΄ animate="μƒνƒœ"둜 μΆ©λΆ„ν•©λ‹ˆλ‹€.
  • ν•˜μ§€λ§Œ μ‚¬μš©μž μž…λ ₯, 쑰건문, μ—¬λŸ¬ μƒνƒœμ—μ„œ μ œμ–΄ν•΄μ•Ό ν•  λ•ŒλŠ” λ°˜λ“œμ‹œ useAnimation()을 μ‚¬μš©ν•˜λŠ” 것이 μœ λ¦¬ν•©λ‹ˆλ‹€.
profile
YOLO

0개의 λŒ“κΈ€