Scroll Animations Explained: How to Add Stunning Effects to Your Website
Learn how to add engaging scroll animations to your website to captivate
users and enhance the browsing experience. This guide covers essential
tips and techniques for creating smooth, interactive effects. Below is
the source code for this in HTML, CSS, and JavaScript.
HTML Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>On-Scroll Animation - Sahil Haryana</title>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://unpkg.com/aos@next/dist/aos.css" />
</head>
<body>
<div class="container-body" data-aos="fade-in" data-aos-duration="4000">
<h1>On-Scroll Animation - By Sahil Haryana</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Adipisci modi quia maiores expedita doloremque culpa quidem eveniet incidunt corporis ratione voluptatem dolorem, commodi ex! Rem, iste incidunt. Facilis, minima quaerat?</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Placeat amet, est quo adipisci, odio aspernatur nostrum modi dolore quisquam illum dignissimos saepe blanditiis nam, unde earum? A modi id alias.</p>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Autem magnam cumque doloribus incidunt repudiandae aspernatur cupiditate placeat nemo amet consequuntur quidem voluptates aut, fuga, reiciendis ratione est iusto possimus nesciunt?</p>
</div>
<main>
<div class="container" data-aos="fade" data-aos-duration="4000">
<h1>On-Scroll Animation</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Adipisci modi quia maiores expedita doloremque culpa quidem eveniet incidunt corporis ratione voluptatem dolorem, commodi ex! Rem, iste incidunt. Facilis, minima quaerat?</p>
</div>
<div class="container" data-aos="fade-up" data-aos-duration="4000">
<h1>On-Scroll Animation</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Adipisci modi quia maiores expedita doloremque culpa quidem eveniet incidunt corporis ratione voluptatem dolorem, commodi ex! Rem, iste incidunt. Facilis, minima quaerat?</p>
</div>
<div class="container" data-aos="fade-down" data-aos-duration="4000">
<h1>On-Scroll Animation</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Adipisci modi quia maiores expedita doloremque culpa quidem eveniet incidunt corporis ratione voluptatem dolorem, commodi ex! Rem, iste incidunt. Facilis, minima quaerat?</p>
</div>
<div class="container" data-aos="fade-left" data-aos-duration="4000">
<h1>On-Scroll Animation</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Adipisci modi quia maiores expedita doloremque culpa quidem eveniet incidunt corporis ratione voluptatem dolorem, commodi ex! Rem, iste incidunt. Facilis, minima quaerat?</p>
</div>
<div class="container-body" data-aos="fade-right" data-aos-duration="4000">
<h1>On-Scroll Animation - By Sahil Haryana</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Adipisci modi quia maiores expedita doloremque culpa quidem eveniet incidunt corporis ratione voluptatem dolorem, commodi ex! Rem, iste incidunt. Facilis, minima quaerat?</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Placeat amet, est quo adipisci, odio aspernatur nostrum modi dolore quisquam illum dignissimos saepe blanditiis nam, unde earum? A modi id alias.</p>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Autem magnam cumque doloribus incidunt repudiandae aspernatur cupiditate placeat nemo amet consequuntur quidem voluptates aut, fuga, reiciendis ratione est iusto possimus nesciunt?</p>
</div>
<div class="container" data-aos="fade-up-right" data-aos-duration="4000">
<h1>On-Scroll Animation</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Adipisci modi quia maiores expedita doloremque culpa quidem eveniet incidunt corporis ratione voluptatem dolorem, commodi ex! Rem, iste incidunt. Facilis, minima quaerat?</p>
</div>
<div class="container" data-aos="fade-up-left" data-aos-duration="4000">
<h1>On-Scroll Animation</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Adipisci modi quia maiores expedita doloremque culpa quidem eveniet incidunt corporis ratione voluptatem dolorem, commodi ex! Rem, iste incidunt. Facilis, minima quaerat?</p>
</div>
<div class="container" data-aos="fade-down-right" data-aos-duration="4000">
<h1>On-Scroll Animation</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Adipisci modi quia maiores expedita doloremque culpa quidem eveniet incidunt corporis ratione voluptatem dolorem, commodi ex! Rem, iste incidunt. Facilis, minima quaerat?</p>
</div>
<div class="container" data-aos="fade-down-left" data-aos-duration="4000">
<h1>On-Scroll Animation</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Adipisci modi quia maiores expedita doloremque culpa quidem eveniet incidunt corporis ratione voluptatem dolorem, commodi ex! Rem, iste incidunt. Facilis, minima quaerat?</p>
</div>
<div class="container-body" data-aos="flip-up" data-aos-duration="4000">
<h1>On-Scroll Animation - By Sahil Haryana</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Adipisci modi quia maiores expedita doloremque culpa quidem eveniet incidunt corporis ratione voluptatem dolorem, commodi ex! Rem, iste incidunt. Facilis, minima quaerat?</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Placeat amet, est quo adipisci, odio aspernatur nostrum modi dolore quisquam illum dignissimos saepe blanditiis nam, unde earum? A modi id alias.</p>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Autem magnam cumque doloribus incidunt repudiandae aspernatur cupiditate placeat nemo amet consequuntur quidem voluptates aut, fuga, reiciendis ratione est iusto possimus nesciunt?</p>
</div>
<div class="container" data-aos="flip-down" data-aos-duration="4000">
<h1>On-Scroll Animation</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Adipisci modi quia maiores expedita doloremque culpa quidem eveniet incidunt corporis ratione voluptatem dolorem, commodi ex! Rem, iste incidunt. Facilis, minima quaerat?</p>
</div>
<div class="container" data-aos="flip-right" data-aos-duration="4000">
<h1>On-Scroll Animation</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Adipisci modi quia maiores expedita doloremque culpa quidem eveniet incidunt corporis ratione voluptatem dolorem, commodi ex! Rem, iste incidunt. Facilis, minima quaerat?</p>
</div>
<div class="container" data-aos="flip-left" data-aos-duration="4000">
<h1>On-Scroll Animation</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Adipisci modi quia maiores expedita doloremque culpa quidem eveniet incidunt corporis ratione voluptatem dolorem, commodi ex! Rem, iste incidunt. Facilis, minima quaerat?</p>
</div>
</main>
</body>
</html>
// JavaScript code goes here
<script src="https://unpkg.com/aos@next/dist/aos.js"></script>
<script>
AOS.init();
</script>
A brief explanation about How to Animate your website while Scrolling
Here's a simple explanation of the code structure for adding on-scroll animations:
HTML: This code uses the AOS (Animate On Scroll) library to apply animations to each section. Every <div> with data-aos attributes (like fade, fade-up, flip-up) tells AOS which animation to use. The data-aos-duration="4000" attribute sets the animation speed to 4 seconds.
CSS: Basic styles are applied here, like centering content, setting background colors, and applying shadows. The .container and .container-body classes define the style and layout for each animated section, giving each a background, padding, and shadow effect.
JavaScript: The AOS library is initialized with AOS.init() in the <script> tag. This line ensures all elements with data-aos attributes animate smoothly as the user scrolls.
This simple setup lets you add engaging scroll animations with minimal code.
Installation
First, you need to install the AOS library. You can do this by linking the following stylesheet in your <head>: