Hi, Developer! Learn from this article how to add scrolling animation effects to any element. With React's AOS library, you can give any part of your website a cool animation as users scroll, which makes the website better and also increases user engagement.
Let's get started!
Follow these steps to implement the scrolling animation.
- Install the AOS library in your project
- Import the library and CSS in Your Specific File
- Initialize the AOS Library
- Implement animation in an element with the data-aos attribute.
Step 1: Install AOS Library into Your Project
// For yarn 👇
yarn add aos
// For npm 👇
npm install aos --save
// For bower 👇
bower install aos --save
Step 2: Import the library and CSS in your specific file component.
import AOS from 'aos';
import 'aos/dist/aos.css';
Step 3: Initialize the AOS Library
For functional components:
useEffect(() => {
AOS.init({
duration: 500,
});
}, [])
For Class Components:
componentDidMount() {
AOS.init({
duration: 500,
});
}
Step 4: Implement animation in an element with the data-aos attribute.
<div data-aos="fade-down" data-aos-easing="linear" data-aos-duration="1500"> ..... </div>
import React, { useEffect } from 'react';
import AOS from 'aos';
import 'aos/dist/aos.css';
function App() {
useEffect(() => {
AOS.init({
duration: 800, // Animation duration in milliseconds
offset: 100, // Offset (in pixels) from the top of the page
easing: 'ease-in-out', // Easing type for the animation
delay: 200, // Delay (in milliseconds) before the animation starts
once: true // If true, the animation will only happen once
});
}, []);
return (
<h1 data-aos="fade-up">
Congratulation we did this
</h1>
);
};
export default App;
Conclusion
I trust it provides value to your React project. Scroll effects are the best way to include amazing animations on your landing page while encouraging your product or web app skills. The AOS library is useful, and I recommend you try it.