Build a Youtube,Twitter,Instagram Subscribers or Followers Animated Counter in Browser Using HTML5 CSS3 and Javascript Full Project For Beginners

 

 

 

index.html

 

 

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Animated Counter in JS</title>
    <link rel="stylesheet"  href="../assets/css/animatedCounter.css">
    <link rel="stylesheet"
          href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/all.min.css"
          integrity="sha256-mmgLkCYLUQbXn0B1SRqzHar6dCnv9oZFPEC1g1cwlkk="
          crossorigin="anonymous" />
  </head>
  <body>
    <header>
      <h1>Happy Coding !</h1>
    </header>
    <section class="counters">
      <div class="container">

        <div>
            <img src="https://media3.giphy.com/media/H8DuBFems6d7UDmamS/source.gif">
            <h2><button class="btn " onclick="start()"> start counting</button></h2>
            </div>
       
        
        <div>
          <i class="fab fa-youtube fa-4x"></i>
          <div class="counter" data-target = "600000">0</div>
          <h3>Subscribers</h3>
        </div>
        
        <div>
          <i class="fab fa-twitter fa-4x"></i>
          <div class="counter" data-target = "20000">0</div>
          <h3>Followers</h3>
        </div>
        
        <div>
          <i class="fab fa-instagram fa-4x"></i>
          <div class="counter" data-target = "3600000">0</div>
          <h3>Likes</h3>
        </div>
        
        <div>
          <i class="fab fa-linkedin fa-4x"></i>
          <div class="counter" data-target = "100000">0</div>
          <h3>Connections</h3>
        </div>
      
   
    </div>

    
   
    </section>

    <script src="../assets/js/animatedCounter.js"></script>
  </body>
</html>

 

 

 

animatedcounter.css

 

 

@import url("https://fonts.googleapis.com/css?family=Roboto&display=swap");

* {
  box-sizing:content-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: "Roboto", sans-serif;
  background: lightskyblue url("../Images/animatedCounter.jpeg");
  background-repeat: no-repeat;
  background-size: cover;
  background-position: center;
  color: #fff;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  height: 100vh;
}
img{
    height:100px;
    width:80px;
    margin: 0px 7px;
}

.btn{
    position:relative;
    background-color: white;
    border: 4px outset green ;
    margin:0px 2px;
    padding: 3px 3px;
}
.btn:hover {
    background-color:coral ;
    color: white;
}

.container {
  max-width: 1100px;
  margin: 0px auto;
  overflow: auto;
  max-height: 350px;
  box-shadow: 20px 20px 25px grey;
  border: outset;
}
section{
  margin:auto 70px;

}
header {
  flex: 1;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

header h1 {
  font-size: 4rem;
  text-shadow: 3px 4px 7px black;
}

.counters {
  background-color: white;
  color: black;
  padding: 20px 20px;
  border-top: 3px black solid;
}

.counters .container {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  grid-gap: 30px;
  text-align: center;
}

.counters i {
  margin-bottom: 5px;
}

.counters .counter {
  font-size: 35px;
  margin: 10px 0;
}

h1:hover {
  transform: scale(1.2);
  color: red;
}

@media (max-width: 700px) {
  .counters .container {
    grid-template-columns: repeat(2, 1fr);
  }

  .counters .container > div:nth-of-type(1),
  .counters .container > div:nth-of-type(2) {
    border-bottom: 1px black solid;
    padding-bottom: 20px;
  }
}

.fa-youtube {
  color: red;
}

.fa-twitter {
  color: deepskyblue;
}

.fa-instagram {
  background-image: linear-gradient(45deg, red, blue);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

.fa-linkedin {
  color: black;
}

h3:hover {
  transform: rotateZ(180deg);
  color: darkOrange;
}

@media (max-width: 450px) {
  header h1 {
    font-size: 3rem;
  }
}

@media (max-width: 360px) {
  header h1 {
    font-size: 2.5rem;
  }

  .counters .counter {
    font-size: 1.5rem;
  }
}

 

 

animatedcounter.js

 

 

const counters = document.querySelectorAll('.counter')
const speed = 1000

function start(){
counters.forEach(counter => {
  const updateCount = () => {

    const target = +counter.getAttribute('data-target')
    console.log("Targeting",target);

    const count = +counter.innerText
    console.log(count);

    const inc = target / speed
    console.log("Incrementing", inc);

    if(count < target) {
      counter.innerText = count + inc
      setTimeout(updateCount, 1)
    } else {
      counter.innerText = target
    }

  }

  updateCount()
})
}

 

 

 

 

 

Leave a Reply