ADVERTISEMENT

Number Counting Animation using HTML, CSS & JavaScript

Telegram Group Join Now

ADVERTISEMENT

Number Counting Animation using HTML, CSS & JavaScript

Hello everyone. Welcome to today’s tutorial on Codewithrandom. We’ll learn how to make Number Counting Animation which is counter using an animation just like the subscriber counter of Youtube it will work just like that. In Today’s session, We will use HTML, CSS, and JavaScript to complete this Number Counting Animation Project.

ADVERTISEMENT

Project Description

ADVERTISEMENT

Step 1
The HTML (Hypertext Markup Language) will help us to create the structure for the list with some necessary attributes and elements to make Number Counting Animation Project.

Step 2
Then we will use CSS (Cascading Stylesheet) which will help us to style or design the project with suitable padding and alignment in the Number Counting Animation Project.

ADVERTISEMENT

Step 3
At last we will use JS (JavaScript) which will add a logic to make the Number Counting Animation Project responsive from the user end.

ADVERTISEMENT

I hope you have got an idea about the project.

HTML Code for Number Counting Animation

First we’ll start with creating the structure of the Number Counting Animation project for that as you can see the above code we have used all the necessary elements & attributes to setup the structure. Let us know code the CSS part to add styling and aligned the tags.

<html lang="en">
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Count Up Animation</title>
    <!-- FontAwesome Icons -->
    <link
      rel="stylesheet"
      href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta2/css/all.min.css"
    />
    <!-- Google Fonts -->
    <link
      href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap"
      rel="stylesheet"
    />
    <!-- Stylesheet -->
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div class="wrapper">
      <div class="container">
        <i class="fas fa-utensils"></i>
        <span class="num" data-val="400">000</span>
        <span class="text">Meals Delivered</span>
      </div>

      <div class="container">
        <i class="fas fa-smile-beam"></i>
        <span class="num" data-val="340">000</span>
        <span class="text">Happy Customers</span>
      </div>

      <div class="container">
        <i class="fas fa-list"></i>
        <span class="num" data-val="225">000</span>
        <span class="text">Menu Items</span>
      </div>

      <div class="container">
        <i class="fas fa-star"></i>
        <span class="num" data-val="280">000</span>
        <span class="text">Five Stars</span>
      </div>
    </div>
    <!-- Script -->
    <script src="script.js"></script>
  </body>
</html>

50+ HTML, CSS & JavaScript Projects With Source Code

ADVERTISEMENT

CSS Code for Number Counting Animation

Second comes the CSS code in which we have styled for the structure we have padded as well as aligned the Number Counting Animation project so that it is properly situated and doesn’t get messy with suitable CSS elements. Now lets code the JavaScript part to make responsive.

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  font-family: "Poppins", sans-serif;
}
body {
  background-color: #121317;
}
.wrapper {
  position: absolute;
  width: 80vw;
  transform: translate(-50%, -50%);
  top: 50%;
  left: 50%;
  display: flex;
  justify-content: space-around;
  gap: 10px;
}
.container {
  width: 28vmin;
  height: 28vmin;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  padding: 1em 0;
  position: relative;
  font-size: 16px;
  border-radius: 0.5em;
  background-color: #21242b;
  border-bottom: 10px solid #18f98f;
}
i {
  color: #18f98f;
  font-size: 2.5em;
  text-align: center;
}
span.num {
  color: #ffffff;
  display: grid;
  place-items: center;
  font-weight: 600;
  font-size: 3em;
}
span.text {
  color: #e0e0e0;
  font-size: 1em;
  text-align: center;
  pad: 0.7em 0;
  font-weight: 400;
  line-height: 0;
}
@media screen and (max-width: 1024px) {
  .wrapper {
    width: 85vw;
  }
  .container {
    height: 26vmin;
    width: 26vmin;
    font-size: 12px;
  }
}
@media screen and (max-width: 768px) {
  .wrapper {
    width: 90vw;
    flex-wrap: wrap;
    gap: 30px;
  }
  .container {
    width: calc(50% - 40px);
    height: 30vmin;
    font-size: 14px;
  }
}
@media screen and (max-width: 480px) {
  .wrapper {
    gap: 15px;
  }
  .container {
    width: 100%;
    height: 25vmin;
    font-size: 8px;
  }
}

JavaScript Code for Number Counting Animation

Last stage of the project the JavaScript in which we have added the logical and coded as per the requirement with some conditions. We have set a timer for interval that is to break the counter and we have used mathfloor to start the count. Let us see the Final Output of the project Number Counting Animation using HTML, CSS & JavaScript (Source Code).

100+ JavaScript Projects With Source Code ( Beginners to Advanced)

ADVERTISEMENT

let valueDisplays = document.querySelectorAll(".num");
let interval = 4000;

valueDisplays.forEach((valueDisplay) => {
  let startValue = 0;
  let endValue = parseInt(valueDisplay.getAttribute("data-val"));
  let duration = Math.floor(interval / endValue);
  let counter = setInterval(function () {
    startValue += 1;
    valueDisplay.textContent = startValue;
    if (startValue == endValue) {
      clearInterval(counter);
    }
  }, duration);
});

Final Output


We have successfully created our Number Counting Animation using HTML, CSS & JavaScript. You can use this project for your personal needs and the respective lines of code are given with the code pen link mentioned above.

ADVERTISEMENT

If you find out this Blog helpful, then make sure to search code random on google for Front End Projects with Source codes and make sure to Follow the Code with Random Instagram page.

Code Idea – codingartist

Written By – Harsh Sawant

Code By – @harshh9

ADVERTISEMENT

ADVERTISEMENT

Telegram Group Join Now

Share on:

Leave a Comment

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT