Mastering the Art: Crafting a Circle Progress Bar with Count Number using HTML, CSS, JS, and SVG @rayen-code


Step into the world of dynamic visuals as we guide you through the creation of a captivating Circle Progress Bar accompanied by a count number. This tutorial dives into the harmonious blend of HTML, CSS, JavaScript, and SVG to bring to life a sleek and interactive progress indicator for your web projects.




Step 1: HTML Markup:

Start by setting up the HTML structure. Define the elements required for the progress bar, count number, and any additional components that contribute to the overall user experience.
<!-- -------------------- HTML -------------------- -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Progress Bar</title>
    <!----------------- link css ------------------->
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="wrapper">
        <div class="outer-cercle">
            <div class="inner-cercle">
                <span id="number">80%</span>
            </div>
        </div>
        <svg class="svg" width="200" height="200" xmlns="htmp://www.w3.org/2000/svg">
            <defs>
                <linearGradient id="svgColor">
                    <stop offset="0%" stop-color="#ff5f6d"></stop>
                    <stop offset="100%" stop-color="#ffc371"></stop>
                </linearGradient>
            </defs>
            <circle class="svg-circle" cx="100" cy="100" r="85"></circle>
        </svg>
    </div>

<!----------------- link java script ------------------->
    <script src="main.js"></script>
</body>
</html>

Step 2: Styling with CSS:

Define the styles in CSS to create an elegant and visually appealing progress bar. Customize colors, sizes, and animations to achieve the desired look and feel.
/*-------------------- CSS --------------------*/
*{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: sans-serif;
}
body {
    min-height: 100vh;
    background: hsl(223, 17%, 24%);
    display: grid;
    place-items: center;
}
.wrapper {
    position: relative;
}
.outer-cercle {
    width: 200px;
    aspect-ratio: 1;
    box-shadow: 
        7px 7px 10px hsl(223, 17%, 19%),
        -7px -7px 10px hsl(223, 17%, 29%)
    ;
    border-radius: 50%;
    display: grid;
    place-items: center;
}
.inner-cercle {
    width: 140px;
    aspect-ratio: 1;
    box-shadow: 
       inset 7px 7px 10px hsl(223, 17%, 19%),
       inset  -7px -7px 10px hsl(223, 17%, 29%)
    ;
    border-radius: 50%;
    display: grid;
    place-items: center;
    color: white;
    font-weight: bold;
    font-size: 2em;
}

.svg {
    position: absolute;
    left: 0;
    top: 0;
}
.svg-circle {
    fill: none;
    stroke: url(#svgColor);
    stroke-width: 30px;
    stroke-dasharray: 535;
    stroke-dashoffset: 535;
    stroke-linecap: round;
    animation: anime 2s linear forwards;
}

@keyframes anime {
    100% {
        stroke-dashoffset: 107;
    }
}   

Step 3: Adding JavaScript Interaction:

Implement the dynamic behavior of the progress bar using JavaScript. Calculate the progress percentage, update the SVG elements, and display the count number.
//-------------------- Java Script --------------------//
let countNumber = document.querySelector("#number");

let number = 0;

setInterval(()=>{
    if(number == 80){
        clearInterval
    }else{
        number += 1;
        countNumber.innerHTML = `${number}%`
    }
}, (2000 / 80) )

In conclusion, crafting a Circle Progress Bar with a count number involves the seamless integration of HTML, CSS, JavaScript, and SVG. The result is not just a visual element; it's an interactive indicator that enhances user engagement.

0 Comments