A circular progress indicator with animated fill and percentage counter
<div class="progress-container">
<div class="progress-circle">
<svg class="progress-ring" width="120" height="120">
<circle class="progress-ring-circle-bg" stroke="#ddd" stroke-width="8" fill="transparent" r="50" cx="60" cy="60"/>
<circle class="progress-ring-circle" stroke="#6a11cb" stroke-width="8" fill="transparent" r="50" cx="60" cy="60"
stroke-dasharray="314.16" stroke-dashoffset="314.16"/>
</svg>
<div class="progress-text">0%</div>
</div>
</div>
.progress-container {
display: flex;
flex-direction: column;
align-items: center;
gap: 20px;
}
.progress-circle {
position: relative;
display: flex;
justify-content: center;
align-items: center;
}
.progress-ring {
transform: rotate(-90deg);
}
.progress-ring-circle-bg {
opacity: 0.3;
}
.progress-ring-circle {
transition: stroke-dashoffset 0.5s ease-in-out;
}
.progress-text {
position: absolute;
font-size: 24px;
font-weight: bold;
color: var(--text-primary);
}
.progress-controls {
display: flex;
gap: 10px;
margin-top: 10px;
}
.progress-btn {
padding: 8px 12px;
background: linear-gradient(135deg, #6a11cb, #2575fc);
border: none;
border-radius: 4px;
color: white;
font-weight: 500;
cursor: pointer;
transition: all 0.2s ease;
}
.progress-btn:hover {
transform: translateY(-2px);
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
/* Dark mode styles */
@media (prefers-color-scheme: dark) {
.progress-ring-circle-bg {
stroke: #444;
}
.progress-ring-circle {
stroke: #8a3eff;
}
}
/**
* Set the progress value of the circular progress bar
* @param {number} percent - The progress percentage (0-100)
*/
function setProgress(percent) {
// Find the progress circle element
const circle = document.querySelector('.progress-ring-circle');
const text = document.querySelector('.progress-text');
if (!circle || !text) return;
// Calculate the circumference
const radius = circle.getAttribute('r');
const circumference = 2 * Math.PI * radius;
// Update the circle stroke offset based on percentage
const offset = circumference - (percent / 100 * circumference);
circle.style.strokeDasharray = `${circumference} ${circumference}`;
circle.style.strokeDashoffset = offset;
// Update the text with animation
animateCounter(text, parseInt(text.textContent) || 0, percent);
}
/**
* Animate the percentage counter
* @param {Element} element - The element containing the percentage text
* @param {number} start - Starting percentage
* @param {number} end - Target percentage
*/
function animateCounter(element, start, end) {
let current = start;
const increment = end > start ? 1 : -1;
const duration = 500; // ms
const steps = Math.abs(end - start);
const stepTime = steps > 0 ? duration / steps : duration;
const timer = setInterval(() => {
current += increment;
element.textContent = `${current}%`;
if ((increment > 0 && current >= end) ||
(increment < 0 && current <= end)) {
element.textContent = `${end}%`;
clearInterval(timer);
}
}, stepTime);
}
// Initialize with a starting value
document.addEventListener('DOMContentLoaded', () => {
// Set initial progress to 0%
setProgress(0);
// If you want to add control buttons
const controls = document.querySelectorAll('.progress-btn');
if (controls.length) {
controls.forEach(btn => {
btn.addEventListener('click', () => {
const value = parseInt(btn.dataset.value);
setProgress(value);
});
});
} else {
// Demonstrate animation if no controls exist
setTimeout(() => setProgress(75), 500);
}
});