I followed a tutorial on how to create a circular progress bar but when copying the div into multiple progress bars with different values, the script only works for the first value. How would I be able to change the script in order to have multiple progress bars work with different values?
I think because document.querySelector is creating a node list but not sure what the best way would be to loop through each item to work for multiple progress circles.
let circularProgress = document.querySelector(".circular-progress"),
progressValue = document.querySelector(".progress-value");
let progressStartValue = 0,
progressEndValue = progressValue.innerText,
speed = 10;
let progress = setInterval(() => {
progressStartValue++;
progressValue.textContent = `${progressStartValue}%`
circularProgress.style.background = `conic-gradient(#7d2ae8 ${progressStartValue * 3.6}deg, #ededed 0deg)`;
if(progressStartValue == progressEndValue) {
clearInterval(progress);
}
}, speed);
*{
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Poppins', sans-serif;
}
body{
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
background: #7d2ae8;
}
.container{
display: flex;
width: 420px;
padding: 50px 0;
border-radius: 8px;
background: #fff;
row-gap: 30px;
flex-direction: column;
align-items: center;
}
.circular-progress{
position: relative;
height: 250px;
width: 250px;
border-radius: 50%;
background: conic-gradient(#7d2ae8 3.6deg, #ededed 0deg);
display: flex;
align-items: center;
justify-content: center;
}
.circular-progress::before{
content: "";
position: absolute;
height: 210px;
width: 210px;
border-radius: 50%;
background-color: #fff;
}
.progress-value{
position: relative;
font-size: 40px;
font-weight: 600;
color: #7d2ae8;
}
.text{
font-size: 30px;
font-weight: 500;
color: #606060;
}
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Test</title>
<link rel="stylesheet" href="./css/stylesheet.css">
</head>
<body>
<div class="container">
<div class="circular-progress">
<span class="progress-value">
50
</span>
</div>
<span class="text">HTML & CSS</span>
</div>
<div class="container">
<div class="circular-progress">
<span class="progress-value">
67
</span>
</div>
<span class="text">HTML & CSS</span>
</div>
<div class="container">
<div class="circular-progress">
<span class="progress-value">
80
</span>
</div>
<span class="text">HTML & CSS</span>
</div>
<!-- JavaScript -->
<script src="./js/script.js"></script>
</body>
</html>