Have you ever noticed how some websites reveal their content gradually as you scroll down the page?
Instead of displaying all elements at once, headings, buttons, images, and other elements appear with smooth animations.
This technique is often called Staggered Animations.
In this tutorial, I'll show you how to create this effect in a React application using a lightweight JavaScript library called AOS (Animate On Scroll).
I'll use a real example from my Cosmetics website to demonstrate how different elements can appear with different animation delays.
What Is AOS?
AOS stands for Animate On Scroll.
It's a JavaScript library that allows developers to animate elements when they enter the viewport.
Instead of implementing custom scroll event listeners, we can simply add HTML data attributes to the elements we want to animate.
For example:
jsx
<h1 data-aos="fade-up">
Welcome to Our Website
</h1>
This makes the heading appear with a fade-up animation when it enters the viewport.
Installing AOS in React
First, install the library:
bash
npm install aos
Next, import AOS and its CSS file into your React application:
jsx
import React, { useEffect } from 'react';
import Aos from 'aos';
import 'aos/dist/aos.css';
Then initialize the library inside your main component:
jsx
useEffect(() => {
Aos.init({
duration: 1000,
});
}, []);
Let's understand the configuration:
duration: 1000 sets the animation duration to 1000 milliseconds.
Aos.init() initializes the animation library.
useEffect() runs the initialization after the component mounts.
[] ensures the effect runs only once per mount.
Now AOS is ready to use throughout the application.
Understanding Staggered Animations
One interesting feature of AOS is the ability to control when individual elements start their animations.
We can achieve this using:
jsx
data-aos-delay="500"
The delay value is measured in milliseconds.
For example:
jsx
<div data-aos="fade-up" data-aos-delay="200">
First Element
</div>
<div data-aos="fade-up" data-aos-delay="400">
Second Element
</div>
<div data-aos="fade-up" data-aos-delay="600">
Third Element
</div>
Each element has the same animation, but a different delay.
This creates a sequential visual effect rather than making all elements animate simultaneously.
A Real Example from My React Project
In my Cosmetics website, I used this technique inside the Hero section.
The goal was to make the content appear gradually and create a more engaging first impression.
Here is a simplified version of the implementation:
jsx
function Hero() {
return (
<section>
<hgroup
data-aos="fade-up"
data-aos-delay="500"
>
<h1>
Enhance Your Natural Beauty
</h1>
<p>
Discover beauty and skincare treatments.
</p>
</hgroup>
<nav
data-aos="fade-up"
data-aos-delay="600"
>
<a href="#appointment">
Book a Consultation
</a>
<a href="#services">
Explore Services
</a>
</nav>
<aside
data-aos="fade-up"
data-aos-delay="800"
>
<p>2,500+ Happy Clients</p>
<p>15+ Specialists</p>
<p>50+ Treatments</p>
<p>10+ Years</p>
</aside>
<figure
data-aos="fade-up"
data-aos-delay="700"
>
<img
src="/images/hero.png"
alt="Cosmetic treatment"
/>
</figure>
</section>
);
}
Notice that I didn't apply the animation to every individual element.
Instead, I applied it to groups of related elements using semantic HTML elements such as hgroup, nav, aside, and figure.
This keeps the JSX cleaner and makes the animation behavior easier to manage.
Analyzing the Animation Delays
Let's look at the actual delay values used in my project:
Heading & Description: fade-up — 500ms
Navigation Buttons: fade-up — 600ms
Hero Image: fade-up — 700ms
Statistics: fade-up — 800ms
All four elements use the same animation, but their delays create a layered visual experience.
One important detail: the delays are relative to when each element's animation is triggered, so the actual appearance sequence can also depend on when each element enters the viewport.
This gives developers more control over how content is revealed while scrolling.
Other Animation Effects You Can Use
AOS provides several animation types beyond fade-up.
Here are a few examples:
jsx
<div data-aos="fade-up">
Fade Up
</div>
<div data-aos="fade-right">
Fade Right
</div>
<div data-aos="zoom-in">
Zoom In
</div>
<div data-aos="flip-left">
Flip Left
</div>
You can experiment with different effects depending on the design of your website.
For example, fade animations work well for text, while zoom animations can be useful for images and visual cards.
Some Tips from My Experience
After working with AOS in my React project, here are a few things I recommend:
Don't animate every element. Too many animations can make the interface distracting.
Use delays carefully to create a natural visual flow.
Keep animation durations reasonable.
Test your animations on mobile devices.
Consider users who prefer reduced motion.
Animations should improve the user experience, not slow down the interaction.
Final Thoughts
Adding scroll animations to a React application doesn't have to be complicated.
With AOS, we can create engaging visual effects using simple data attributes without writing custom scroll detection logic.
The most interesting part is not simply adding animations, but controlling their timing to create a more natural and enjoyable browsing experience.
Do you prefer using animation libraries like AOS, or do you prefer implementing animations with CSS?
Share your thoughts in the comments!
Top comments (0)