A circular progress bar in JavaScript is a visual representation of progress or completion that takes the form of a circular shape. It is commonly used in web development to provide users with an intuitive and visually appealing way to track the progress of a task or process.
The circular progress bar typically consists of an outer circle representing the total progress or completion and an inner arc or circle that dynamically grows or fills up to indicate the current progress. The progress is usually represented as a percentage, ranging from 0% to 100%.
To create a circular progress bar in JavaScript, you can utilize HTML, CSS, and JavaScript. Here's a step-by-step overview of the process:
Here is an example code snippet that demonstrates this approach:
<!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" /> <title>Document</title> <link rel="stylesheet" href="css/style.css" /> </head> <body> <div class="container"></div> <script src="js/script.js"></script> </body> </html>
const container = document.querySelector(".container"); const courses = [ { course: "HTML", percent: 99, color: "#f9ca24" }, { course: "CSS", percent: 65, color: "#78e08f" }, { course: "JavaScript", percent: 35, color: "#c56cf0" }, { course: "Bootstrap", percent: 85, color: "#badc58" }, ]; courses.forEach((course) => { container.innerHTML += ` <div class="progess-group"> <div class="circular-progress" > <span class="course-value" style="color:${course.color}">0%</span> </div> <label class="text" style="color:${course.color}">${course.course}</label> </div> `; }); //style=" background: conic-gradient(${course.color} ${3.6 * course.percent}deg, #fff 0deg);" const progressGroups = document.querySelectorAll(".progess-group"); progressGroups.forEach((progress, index) => { let progressStartValue = 0; let progressStartEnd = courses[index].percent; let speed = 50; let progessTimer = setInterval(() => { progressStartValue++; if (progressStartValue == progressStartEnd) { clearInterval(progessTimer); } progress.querySelector(".circular-progress").style.background = ` conic-gradient(${courses[index].color} ${3.6 * progressStartValue}deg, #fff 0deg)`; progress.querySelector(".course-value").innerHTML = progressStartValue + "%"; }, speed); });
In summary, this code dynamically generates circular progress bars for different courses and animates them to show the progress percentage using a conic gradient and interval timers.
Finally, you can add some CSS styles to enhance the look and feel of the button and container. you can adjust the CSS styles as desired to match the design of your website.
@import url("https://fonts.googleapis.com/css2?family=Poppins:wght@200;300;400;500;600;700;900&display=swap"); * { font-family: "Poppins", sans-serif; margin: 0; padding: 0; } body { height: 100vh; width: 100vw; display: grid; place-items: center; background-color: #222; } .container { display: flex; gap: 10px; } .progess-group { background-color: #535c68; width: 300px; padding: 50px 0; display: flex; flex-direction: column; align-items: center; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.1) 0px 4px 6px -1px, rgba(0, 0, 0, 0.06) 0px 2px 4px -1px; } .circular-progress { height: 200px; width: 200px; border-radius: 50%; display: flex; justify-content: center; align-items: center; position: relative; transition: 0.5s; } .circular-progress::before { content: ""; position: absolute; width: 180px; height: 180px; border-radius: 50%; background-color: #535c68; } .course-value { position: relative; color: #eb4d4b; font-size: 35px; font-weight: 500; } .text { margin-top: 18px; font-size: 25px; font-weight: 500; letter-spacing: 1px; color: white; }
By combining HTML, CSS, and JavaScript, you can create an interactive and visually appealing circular progress bar that provides users with a clear indication of progress and completion in various applications, such as file uploads, form submissions, or lengthy operations.
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions