스크롤 방향에 따라 움직이는 텍스트, 스크롤 강도에 따라 기울기 적용하기
스크롤에 따라 텍스트가 움직이는 효과를 주면서 스크롤 강도에 따라 텍스트의 기울기가 변화하는 효과를 구현하고 싶었다.


스크롤을 할 만큼의 높이가 필요하다.
<section className="h-[500vh]"></section>
텍스트가 부모 요소의 영역을 넘어가더라도 한 줄로 위치해있어야 한다.
<div className="overflow-hidden h-screen sticky top-0">
<p className="origin-bottom-left whitespace-nowrap uppercase">
{`Nothing in this world can take the place of persistence. Talent will not; nothing is more common than unsuccessful men with talent. Genius
will not; unrewarded genius is almost a proverb. Education will not; the world is full of educated derelicts. Persistence and determination
alone are omnipotent. The slogan 'Press On!' has solved and always will solve the problems of the human race.
`}
</p>
</div>
스크롤이 얼만큼 진행되었는지 알아야한다. 👉🏻 useScroll
useScroll
const { scrollYProgress } = useScroll()
target 속성을 통해 특정 요소에 스크롤 애니메이션을 제한할 수 있다.
useRef를 사용하여 참조된 DOM 요소를 지정한다.
const targetRef = useRef(null)
const { scrollYProgress } = useScroll({
target:targetRef,
offset:['start start', 'end start']
})
<div className="overflow-hidden h-screen sticky top-0">
<p className="h-screen origin-bottom-left whitespace-nowrap uppercase">
{`Nothing in this world can take the place of persistence. Talent will not; nothing is more common than unsuccessful men with talent. Genius
will not; unrewarded genius is almost a proverb. Education will not; the world is full of educated derelicts. Persistence and determination
alone are omnipotent. The slogan 'Press On!' has solved and always will solve the problems of the human race.
`}
</p>
</div>
</section>
scrollYProgress
값의 변화 속도를 계산해야한다. 👉🏻 useVelocity
scrollYProgress
값의 변화 속도를 계산해야한다. 👉🏻 useVelocity
const scrollVelocity = useVelocity(scrollYProgress);
이동한 스크롤만큼 텍스트의 위치를 이동시켜야 한다. 👉🏻 useTransform
const xRaw = useTransform(scrollYProgress, [0, 1], [0, -3000]);
const x = useSpring(xRaw, { mass: 3, stiffness: 400, damping: 50 });
스크롤 속도에 따라 텍스트의 기울기를 변화시켜야 한다.
const skewXRaw = useTransform(scrollVelocity, [-1, 1], ['45deg', '-45deg']);
const skewX = useSpring(skewXRaw, { mass: 3, stiffness: 400, damping: 50 });
skewX, x 값을 텍스트 DOM 요소 스타일에 적용시켜야한다.
<motion.p
style={{ skewX, x }}
className="h-screen origin-bottom-left whitespace-nowrap uppercase">
{`Nothing in this world can take the place of persistence. Talent will not; nothing is more common than unsuccessful men with talent. Genius
will not; unrewarded genius is almost a proverb. Education will not; the world is full of educated derelicts. Persistence and determination
alone are omnipotent. The slogan 'Press On!' has solved and always will solve the problems of the human race.
`}
</motion.p>
Last updated