In this post, I’ll show you how to implement a circular progress bar animation that enhances your website's visual appeal. Let’s get started!
1. HTML Structure
First, we define the HTML structure:
2. CSS Styling
3. JavaScript
Explanation
Understanding the Structure
1. The .loader class creates the container for the animation.
2. The .loader-container holds the SVG circle and the percentage count.
SVG Circle Animation
To create the circular progress bar, we use an SVG circle with these properties:
1. cx="50%", cy="50%" (center positioning – do not change these)
2. r="100" (radius, can be adjusted as needed)
Note: If you modify the radius (r), update the JavaScript calculation accordingly:
How the Animation Works
The animation is controlled using CSS properties:
1. stroke-dasharray: Defines the total length of the stroke (628 for a circle with radius 100).
2. stroke-dashoffset: Initially set to 628 (fully hidden). As the animation progresses, this value decreases, revealing the stroke.
In JavaScript:
1. We calculate progress using circumference - progress.
2. The setTimeout(updateProgress, 15) function updates the progress bar every 15 milliseconds.
3. Customization: Adjust the radius, animation speed, and colors to fit your design needs.
Learn More
For detailed explanations on stroke-dasharray and stroke-dashoffset, check out this MDN Web Docs reference.
Conclusion
We successfully created a circular progress bar animation using plain HTML, CSS, and JavaScript! 🎉 Try experimenting with different values to customize it for your project.