"CSS is beautiful but hard".
Most developers can agree on that statement. In fact many backend developers left the frontend because of the difficulty it posed. CSS Animation is one of the most difficult part of CSS. Well, there is good news.
Developers have been developing libraries to help us boycott writing too many CSS than is necessary. That is Awesome!!!
In this article, I will be introducing you to one of those libraries. It is called AOS (Animation on Scroll).
AOS is a free library on github which not only helps you animate your website but also ensure the animation only happens when you have scrolled to that part of the website.
Without further talks, let's get practical. I will be demonstrating how to use it on React and plain JavaScript projects.
Plain JavaScript
Starter Project
Get the React starter Project here
Setting Up and Initializing
- Add the following CSS link to the head tag in the
index.html
<link href="https://unpkg.com/aos@2.3.1/dist/aos.css" rel="stylesheet">
- Add the following script just before the closing body tag
<script src="https://unpkg.com/aos@2.3.1/dist/aos.js"></script>
- In the
scripts.js
file, add the following code to initialize AOS
AOS.init();
Nav Animation
Add data-aos="fade-right"
to the nav
section to make it fade in from the right like so:
<!-- nav -->
<nav class="navbar navbar-default" data-aos="fade-right">
<div class="navbar-header">
<h1>Navigation</h1>
</div>
</nav>
You can check it out in your browser
AOS offers us other options like the normal CSS animation. So, let's improve the flow and slow it a little. Our
nav
now looks like this
<!-- nav -->
<nav
class="navbar navbar-default"
data-aos="fade-right"
data-aos-delay="50"
data-aos-duration="1000"
data-aos-easing="ease-in-out-cubic"
>
<div class="navbar-header">
<h1>Navigation</h1>
</div>
</nav>
You See that the nav
animation is smoother? That's the beauty!
Now you get the point. Animate the other part of the web page as you desire
Final Plain JS Code
React
Starter Project
Get the React starter Project here
Setting Up and Initializing
- Install AOS with the following code
npm install aos --save
- Import and Initialize AOS in the
App.js
file with the following code
import AOS from "aos";
import "aos/dist/aos.css";
AOS.init();
Nav Animation
Add data-aos="flip-left"
to the nav
section to make it flips left like so:
<!-- nav -->
<nav className="navbar navbar-default" data-aos="flip-left">
<div className="navbar-header">
<h1>Navigation</h1>
</div>
</nav>
You can check it out in your browser
AOS offers us other options like the normal CSS animation. So, let's improve the flow and slow it a little. Our
nav
now looks like this
<!-- nav -->
<nav
className="navbar navbar-default"
data-aos="flip-left"
data-aos-delay="50"
data-aos-duration="2000"
data-aos-easing="ease-in-out-cubic"
>
<div className="navbar-header">
<h1>Navigation</h1>
</div>
</nav>
You See that the nav
animation is smoother? That's the beauty!
Now you get the point. Animate the other part of the web page as you desire
Final React Code
- All codes are here
Conclusion
There are a whole lot you can do with AOS. I encourage you to play around with it and see what result you get.
If you have questions or comments, please drop them in the comment section. See you soon.
Top comments (10)
I want to add something to the conversation.
If you are using React or NextJS
put your
Inside a useEffect like so
Without useEffect it will generate an error saying "document is not defined"
Thank you very much for your contribution.
Nice, declarative animation framework can speed things up tremendously.
Of course, one should understand the fundamentals to be able to build their own custom stuff, but for the average user who just wants to spice up their websites/profiles with a little extra on top without spending hours or days practicing CSS, letting someone else handle it makes things a lot easier.
Yes. Your points are valid.Thank you for taking your time to read through.
Nice Article my bro...it really helped me :)
Thank you.
I am happy you found it helpful
This is beautiful!
It really is. Thank you for going through 🤗
This is Awesome bro!!!
Thank you for taking your time