Design Converter
Education
Last updated on Mar 18, 2025
•6 mins read
Last updated on Mar 11, 2025
•6 mins read
How can you create smooth scroll effects in your React apps?
The useScroll hook from Framer Motion makes it easy to add scroll-based animations to your web pages. It helps create smooth and interactive scrolling experiences, making your app feel more engaging.
Framer Motion is a popular animation library for React. It simplifies complex animations, allowing developers to add dynamic scrolling effects without much hassle. So, how does useScroll work, and how can you use it to improve your project’s user experience?
Let’s break it down.
To get started with useScroll, you need to install Framer Motion in your React project. Here’s how you can do it:
1npm install framer-motion
Once installed, import useScroll from Framer Motion and create a new component to use it. Here’s a basic setup:
1import { motion, useScroll } from 'framer-motion'; 2 3export default function App() { 4 const { scrollYProgress } = useScroll(); 5 6 return ( 7 <div className="h-screen"> 8 <motion.div style={{ scaleX: scrollYProgress }} /> 9 </div> 10 ); 11}
useScroll returns four motion values: scrollX, scrollY, scrollXProgress, and scrollYProgress. scrollX and scrollY represent the absolute scroll position in pixels, while scrollXProgress and scrollYProgress represent the scroll position as a value between 0 and 1. These values can be used to create animations linked to the user’s scroll position.
With useScroll, you can create scroll-linked animations where the animation is linked to the user’s scroll position. You can use other motion value hooks like useTransform and useSpring to create more complex animations.
Here’s an example of creating a scroll-linked animation using useTransform:
1import { motion, useScroll, useTransform } from 'framer-motion'; 2 3export default function ScrollExample() { 4 const { scrollYProgress } = useScroll(); 5 const scale = useTransform(scrollYProgress, [0, 1], [1.05, 1]); 6 7 return ( 8 <motion.div style={{ scale }} /> 9 ); 10}
Scroll-linked animations are animations where the progress or movement of an animation is directly linked to how far the user has scrolled. Examples include parallax effects, fade-in elements, and scale transformations. These animations enhance user engagement by providing a dynamic experience as users scroll through your content.
useScroll can be used to trigger animations when the user scrolls to a specific position. You can use the scrollX and scrollY values to trigger animations when the user scrolls to a specific point. Additionally, scrollXProgress and scrollYProgress can be used to trigger animations when the user scrolls to a specific percentage.
To create more complex animations, you can use useScroll with other Framer Motion features like variants and animation controls. You can also combine useScroll with other React libraries like React Intersection Observer to create more advanced scrolling effects.
When using useScroll, ensure that your React components are optimized for performance. Use memoization and other optimization techniques to reduce the number of re-renders. Keep your components simple and focused on a single task to make them easier to maintain.
For more information on useScroll and other Framer Motion features, check out the official documentation. You can also explore other React libraries like React Intersection Observer to create more advanced scrolling effects. Joining online communities and forums can help you learn from other developers and share your own experiences with useScroll.
To better understand how useScroll works, let's visualize the process using a Mermaid diagram:
This diagram illustrates how the useScroll hook captures scroll events and returns motion values that can be used to create dynamic animations.
Here’s an example of creating a scroll progress indicator using useScroll and useTransform:
1import { motion, useScroll, useTransform } from 'framer-motion'; 2 3export default function ScrollProgress() { 4 const { scrollYProgress } = useScroll(); 5 const strokeDasharray = useTransform(scrollYProgress, [0, 1], [0, 283]); 6 7 return ( 8 <div> 9 <motion.svg> 10 <circle cx="50" cy="50" r="45" stroke="#00FF00" strokeWidth="10" strokeDasharray="283" style={{ strokeDashoffset: strokeDasharray }} /> 11 </motion.svg> 12 </div> 13 ); 14}
To create a scroll-linked dynamic scaling effect for a text element, you can use useScroll and useTransform like this:
1import { motion, useScroll, useTransform } from 'framer-motion'; 2 3export default function ScrollOne() { 4 const { scrollYProgress } = useScroll(); 5 const scale = useTransform(scrollYProgress, [0, 1], [1.05, 1]); 6 7 return ( 8 <section> 9 <motion.div style={{ scale }}> 10 <h1>Smooth Scroll Animation</h1> 11 </motion.div> 12 </section> 13 ); 14}
You can create parallax effects by using useScroll to animate elements based on the user’s scroll position. Here’s a simple example:
1import { motion, useScroll, useTransform } from 'framer-motion'; 2 3export default function ParallaxExample() { 4 const { scrollYProgress } = useScroll(); 5 const translate = useTransform(scrollYProgress, [0, 1], [0, -100]); 6 7 return ( 8 <motion.div style={{ translateY: translate }} /> 9 ); 10}
To create fade-in elements triggered by scrolling, you can use useScroll along with whileInView from Framer Motion:
1import { motion, useScroll } from 'framer-motion'; 2 3export default function FadeInExample() { 4 return ( 5 <motion.div initial={{ opacity: 0 }} whileInView={{ opacity: 1 }}> 6 <h1>Fade-In Animation</h1> 7 </motion.div> 8 ); 9}
You can track the scroll velocity and direction using useMotionValueEvent with useScroll:
1import { motion, useScroll, useMotionValueEvent } from 'framer-motion'; 2import { useState } from 'react'; 3 4export default function ScrollDirection() { 5 const { scrollY } = useScroll(); 6 const [scrollDirection, setScrollDirection] = useState("down"); 7 8 useMotionValueEvent(scrollY, "change", (current) => { 9 const diff = current - scrollY.getPrevious(); 10 setScrollDirection(diff > 0 ? "down" : "up"); 11 }); 12 13 return ( 14 <div> 15 <h1>Scrolling {scrollDirection}</h1> 16 </div> 17 ); 18}
While useScroll is primarily for scroll-linked animations, you can achieve smooth scrolling between sections using the react-scroll package:
1import { Link, animateScroll as scroll } from "react-scroll"; 2 3export default function SmoothScrollExample() { 4 return ( 5 <div> 6 <Link to="section1" smooth={true} duration={500}> 7 Scroll to Section 1 8 </Link> 9 </div> 10 ); 11}
useScroll from Framer Motion makes it easy to add smooth scroll effects to React apps. It helps create animations that respond to scroll position, making the user experience more interactive. From simple scroll-linked effects to more complex triggers at specific points, useScroll gives developers the tools to build engaging web pages.
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.