Background Image Parallax

➡️ Live Demo

import { motion, useScroll, useTransform } from 'framer-motion';
import Image from 'next/image';
import Background from '../../../public/background-2.jpg';

export default function Intro() {
  const { scrollYProgress } = useScroll({
    offset: ['start start', 'end start'],
  }); // 🚧 offset 활용

  // 🚧 스크롤의 진행률(0과 1 사이의 값)은 0vh에서 150vh 사이의 값으로 변환되어 translateY 값으로 사용
  const y = useTransform(scrollYProgress, [0, 1], ['0vh', '150vh']);

  return (
    <div className='h-screen overflow-hidden'>
      <motion.div style={{ y }} className='relative h-full bg-fuchsia-50'>
        <Image src={Background} alt='image' fill className='object-cover' />
      </motion.div>
    </div>
  );
}

Last updated