useScroll is used to create scroll-linked animations, like progress indicators and parallax effects.
const { scrollYProgress } = useScroll()
return <motion.div style={{ scaleX: scrollYProgress }} />useScroll is able to run some animations with the browser's ScrollTimeline API for optimal hardware-accelerated performance, removing scroll measurements, improving scroll synchronisation and ensuring animations remain smooth even under heavy CPI usage.
Usage
Import useScroll from Motion:
import { useScroll } from "motion/react"useScroll returns six motion values:
-
scrollX/Y: The absolute scroll position, in pixels. -
scrollXProgress/YProgress: The scroll position between the defined offsets, as a value between0and1. -
scrollXLength/YLength: The maximum scroll position of thecontainer, in pixels. This is the container's scrollable size minus its visible size.
Page scroll
By default, useScroll tracks the page scroll.
const { scrollY } = useScroll()
useMotionValueEvent(scrollY, "change", (latest) => {
console.log("Page scroll: ", latest)
})