Index.html :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="index.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"
integrity="sha512-iBBXm8fW90+nuLcSKlbmrPcLa0OT92xO1BIsZ+ywDWZCvqsWgccV3gFoRBv0z+8dLJgyAHIhR35VZc2oM/gI1w=="
crossorigin="anonymous" />
<title>Automatic Counter</title>
</head>
<body>
<div class="container">
<div class="box">
<div class="box1">
<i class="fa fa-beer" aria-hidden="true"></i>
<div class="counter" data-target="4000"></div>
<span>Project Completed</span>
</div>
<div class="box1">
<i class="fa fa-exclamation-circle" aria-hidden="true"></i>
<div class="counter" data-target="500">0</div>
<span>Happy Client</span>
</div>
<div class="box1">
<i class="fa fa-bolt" aria-hidden="true"></i>
<div class="counter" data-target="1400">0</div>
<span>On going project</span>
</div>
</div>
<script src="index.js"></script>
</body>
</html>
index.css
*{
padding: 0px;
margin: 0px;
box-sizing: border-box;
font-family:'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
}
.container{
display: flex;
text-align: center;
justify-content: center;
width: 100%;
height: 100vh;
background-color: brown;
}
.box{
display: flex;
text-align: center;
justify-content: center;
flex-direction: row;
margin: 150px 70px;
padding: 50px;
border: 2px solid white;
}
.box1{
display: flex;
flex-direction: column;
padding: 20px;
justify-content: center;
font-size: 20px;
color: white;
}
.counter{
font-size: 100px;
color: rgb(223, 219, 16);
padding: 5px;
}
span{
color: black;
}
.fa{
font-size: 70px;
padding: 10px 0px;
}
index.js:
const counters1 = document.getElementsByClassName("counter"); //gives html collection
const counters = document.querySelectorAll(".counter"); //gives node list
/*
Note : use querySelectorAll instead of getElementsByClassName.
*/
counters.forEach((counter) => {
console.log(counter)
counter.innerHTML = 0
const inc_counter = () => {
initial_counter = Number(counter.innerHTML)
let data_target = +(counter.getAttribute("data-target"))
let incr = data_target / 100;
if (initial_counter < data_target) {
console.log("inisde if");
counter.innerHTML = `${initial_counter + incr}`
setTimeout(inc_counter, 10)
}
}
inc_counter()
});
0 Comments