App Web Tutorials
  • Home
  • Contact
  • Privacy Policy
Reading: Power Your HTML Website with Stunning Animations: Easy Guide for Beginners
Share
Subscribe Now
App Web TutorialsApp Web Tutorials
Font ResizerAa
Search
  • Home
  • Contact
  • Privacy Policy
Follow US
Copyright © 2017-2025 App Web Tutorials. All Rights Reserved.
animationsbeginnersCSSHTMLJavaScriptTutorialwebsite

Power Your HTML Website with Stunning Animations: Easy Guide for Beginners

Chetan
By Chetan
Last updated: January 15, 2025
4 Min Read
How to Add Eye-Catching Animations to Your HTML Website: A Step-by-Step Guide for Beginners
How to Add Eye-Catching Animations to Your HTML Website: A Step-by-Step Guide for Beginners
SHARE

Adding animations to your HTML website can greatly enhance user experience and engagement. In this tutorial, we’ll walk you through the process of incorporating eye-catching animations using CSS and JavaScript. Whether you’re a beginner or have some experience with web development, this guide will provide you with the knowledge and code snippets necessary to bring your website to life.

Step 1: Include the Required Libraries
To get started, you’ll need to include the Animate.css library and jQuery in your HTML file. Add the following lines inside the <head> tag:

<span style="font-family: inherit;"><link href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</span>

Step 2: Add Animation Classes to Elements
Next, identify the elements you want to animate and add the appropriate classes. Use the “u-in-viewport” class to trigger the animation when the element comes into the viewport. Additionally, add the “data-animation” attribute to specify the desired animation effect from the Animate.css library. For example:

<span style="font-family: inherit;"><div class="u-in-viewport" data-animation="fadeIn" data-animation-duration="1000ms" data-animation-delay="500ms">
  <!-- Your content goes here -->
</div></span>

Step 3: Implement the Intersection Observer
To detect when an element enters the viewport, we’ll use the Intersection Observer API. Add the following JavaScript code to your file:

<span style="font-family: inherit;">
<script>
    document.addEventListener('DOMContentLoaded', function () {
        const elements = document.querySelectorAll('.u-in-viewport');

        const observer = new IntersectionObserver((entries) => {
            entries.forEach(entry => {
                if (entry.isIntersecting) {
                    const element = entry.target;
                    const animation = element.getAttribute('data-animation');
                    const duration = element.getAttribute('data-animation-duration') || '1000ms';
                    const delay = element.getAttribute('data-animation-delay') || '0ms';

                    element.style.visibility = 'visible';
                    element.style.animationDuration = duration;
                    element.style.animationDelay = delay;
                    element.classList.add('animate__animated', `animate__${animation}`);

                    observer.unobserve(element); // Stop observing the element once it has been animated
                }
            });
        });

        elements.forEach(element => {
            observer.observe(element);
        });
    });
</script>
  </span>

This code sets up the Intersection Observer to monitor elements with the “u-in-viewport” class. When an element enters the viewport, the appropriate animation classes are added, triggering the animation effect.

Step 4: Animating Numbers
If you want to create an engaging animation for numbers, such as counters, you can use JavaScript. Here’s an example of how to animate a number from 0 to a target value:

<span style="font-family: inherit;"><code style="background: 0px 0px; border: 0px; box-sizing: border-box; display: block; hyphens: none; line-height: 1.5; margin: 0px; outline: 0px; overflow-wrap: normal; padding: 0px; tab-size: 4; text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; vertical-align: baseline; word-break: normal; word-spacing: normal;">
<script>
  function animateNumber(element, targetValue, duration) {
    let currentValue = 0;
    const increment = targetValue / (duration / 16);

    function updateValue() {
      currentValue += increment;
      if (currentValue >= targetValue) {
        currentValue = targetValue;
        clearInterval(interval);
      }
      element.textContent = Math.floor(currentValue);
    }

    updateValue();
    const interval = setInterval(updateValue, 16);
  }

  const options = {
    root: null,
    rootMargin: '0px',
    threshold: 0.9,
  };

  const observer = new IntersectionObserver(function(entries, observer) {
    entries.forEach(function(entry) {
      if (entry.isIntersecting) {
        const element = entry.target;
        const targetValue = Number(element.textContent);
        animateNumber(element, targetValue, 8000);
        observer.unobserve(element);
      }
    });
  }, options);

  const counterElements = document.querySelectorAll(".counter");
  counterElements.forEach(function(element) {
    observer.observe(element);
  });
</script>
  

This code uses the Intersection Observer to detect when an element with the “counter” class enters the viewport. Once triggered, the animateNumber function gradually increments the number from 0 to the target value over a specified duration.

Conclusion:
Adding animations to your HTML website is a great way to capture your visitors’ attention and create a more engaging user experience. By leveraging CSS libraries like Animate.css and utilizing JavaScript techniques such as the Intersection Observer, you can effortlessly incorporate smooth and captivating animations into your website.

We hope this tutorial has provided you with a solid foundation for adding animations to your HTML website. Feel free to explore more animation effects and experiment with different combinations to create a truly unique and visually appealing website.

Remember to test your animations across different devices and browsers to ensure a consistent experience for all users. Happy animating!

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article Master Git with Ease: The Ultimate Cheatsheet Every Developer Needs Master Git with Ease: The Ultimate Cheatsheet Every Developer Needs
Next Article Fixing SSH ‘Unprotected Private Key File’ Error in Windows 11: A Quick Guide Easily Fix the SSH ‘Unprotected Private Key File’ Error on Windows 11 in Minutes
Leave a Comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

FacebookLike
XFollow
InstagramFollow
Most Popular
How to Install and Use pyenv on Windows, Ubuntu, and macOS: A Complete Guide
The Ultimate Guide to Installing pyenv on Windows, Ubuntu, and macOS
January 21, 2025
How to Host a Python Flask App with SSL on WAMP Server in Windows: A Step-by-Step Guide for Beginners
January 21, 2025
Fixing SSH ‘Unprotected Private Key File’ Error in Windows 11: A Quick Guide
Easily Fix the SSH ‘Unprotected Private Key File’ Error on Windows 11 in Minutes
January 20, 2025
How to Add Eye-Catching Animations to Your HTML Website: A Step-by-Step Guide for Beginners
Power Your HTML Website with Stunning Animations: Easy Guide for Beginners
January 15, 2025
Master Git with Ease: The Ultimate Cheatsheet Every Developer Needs
Master Git with Ease: The Ultimate Cheatsheet Every Developer Needs
January 21, 2025

You Might Also Like

Step by Step Tutorial to Convert HTML to PDF using JavaScript
ConvertGenerate PDFHTML to PDFJavaScriptTutorial

How to Instantly Convert HTML to PDF: A Foolproof JavaScript Guide for Beginners

3 Min Read

Always Stay Up to Date

Subscribe to our newsletter to get our newest articles instantly!
App Web Tutorials

We provide tutorials, tips, tricks, and advice for improving programming and development skills.

Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?