Circular progress ring
圆环进度加载
Smoothly stroke a circular progress ring to any percentage for uploads, syncs or timers.
Also searched as: 圆形进度条, 圆环加载百分比, 环状上传进度, 倒计时圆环
- Placement
- Button, Card list
- Mood
- Crisp, Technical
- Trigger
- 状态变化
- Site types
- 工具和应用, 个人网站, 品牌官网
How it works & accessibility
An SVG circle sets stroke-dasharray to the exact circumference and transitions stroke-dashoffset with CSS variables while updating aria-valuenow and an optional percentage label; reduced motion snaps straight to the target value without animation.
Best for: Button, Card list (状态变化).
Reduced motion: 关闭描边过渡动画,百分比更新时圆环和数字直接跳到新位置
Mobile & touch: 与桌面相同,SVG 按容器尺寸等比缩放,线宽在手机上保持清晰
Key parameters
| Parameter | Key | Default | Range | Notes |
|---|---|---|---|---|
| 过渡时长 | duration | 450ms | — | — |
| 缓动 | easing | cubic-bezier(0.22, 1, 0.36, 1) | — | — |
Ready-to-paste AI prompt
Add “Circular progress ring” to the button in my project: An SVG circle sets stroke-dasharray to the exact circumference and transitions stroke-dashoffset with CSS variables while updating aria-valuenow and an optional percentage label; reduced motion snaps straight to the target value without animation.
Parameters: duration: 450ms, easing: cubic-bezier(0.22, 1, 0.36, 1).
Preserve existing content and layout. Respect prefers-reduced-motion by showing the final state. Content must remain visible if JavaScript fails or is disabled.
Reference: https://donglang.linter.top/effects/progress-ring.md (canonical specification and reference code).Dependency-free reference code
CSS
/* 圆环进度加载(Circular progress ring)样式,配合 progress-ring.js。 */
.cpr-ring {
position: relative;
display: inline-grid;
place-items: center;
width: var(--cpr-size, 84px);
height: var(--cpr-size, 84px);
}
.cpr-ring svg {
width: 100%;
height: 100%;
transform: rotate(-90deg);
overflow: visible;
}
.cpr-track,
.cpr-fill {
fill: none;
stroke-width: var(--cpr-stroke, 6);
}
.cpr-track {
stroke: var(--cpr-track-color, color-mix(in srgb, currentColor 14%, transparent));
}
.cpr-fill {
stroke: var(--cpr-color, currentColor);
stroke-linecap: round;
transition: stroke-dashoffset var(--cpr-duration, 450ms) var(--cpr-easing, cubic-bezier(0.22, 1, 0.36, 1));
}
.cpr-value {
position: absolute;
font-variant-numeric: tabular-nums;
font-weight: 700;
font-size: var(--cpr-font-size, 15px);
line-height: 1;
}
.cpr-ring.cpr-reduced .cpr-fill {
transition: none;
}
@media (prefers-reduced-motion: reduce) {
.cpr-ring:not(.cpr-force) .cpr-fill {
transition: none;
}
}JavaScript
// 圆环进度加载(Circular progress ring)参考实现。
// 用法:容器内含 <svg><circle class="cpr-track"/><circle class="cpr-fill" r="40"/></svg>,可选 [data-cpr-value] 显示百分比。
// 调用 const ctl = progressRing(el, { duration: 450, easing: '...', value: 0 }); 后续调 ctl.set(75),销毁调 ctl.destroy()。
const clamp = (n) => Math.min(100, Math.max(0, Number(n) || 0));
export function progressRing(el, {
value = 0,
duration = 450,
easing = 'cubic-bezier(0.22, 1, 0.36, 1)',
format = (v) => `${Math.round(v)}%`,
ignoreReducedMotion = false,
} = {}) {
if (!(el instanceof Element)) return null;
const fill = el.querySelector('.cpr-fill');
if (!fill) return null;
const r = Number(fill.getAttribute('r'));
if (!Number.isFinite(r) || r <= 0) return null;
const initial = value;
const circ = 2 * Math.PI * r;
const valueEl = el.querySelector('[data-cpr-value]');
const reduceQuery = typeof matchMedia === 'function' ? matchMedia('(prefers-reduced-motion: reduce)') : null;
const prevRole = el.getAttribute('role');
const prevMin = el.getAttribute('aria-valuemin');
const prevMax = el.getAttribute('aria-valuemax');
const prevNow = el.getAttribute('aria-valuenow');
const prevText = valueEl ? valueEl.textContent : null;
let current = clamp(initial);
let destroyed = false;
el.classList.add('cpr-ring');
if (ignoreReducedMotion) el.classList.add('cpr-force');
el.style.setProperty('--cpr-duration', `${Number(duration) || 450}ms`);
el.style.setProperty('--cpr-easing', String(easing));
if (!prevRole) el.setAttribute('role', 'progressbar');
if (!prevMin) el.setAttribute('aria-valuemin', '0');
if (!prevMax) el.setAttribute('aria-valuemax', '100');
const apply = (val, immediate) => {
if (destroyed) return;
current = clamp(val);
const offset = circ * (1 - current / 100);
if (immediate) fill.style.transition = 'none';
fill.style.strokeDasharray = `${circ}`;
fill.style.strokeDashoffset = `${offset}`;
el.setAttribute('aria-valuenow', String(Math.round(current)));
if (valueEl) valueEl.textContent = format(current);
if (immediate) {
void fill.getBoundingClientRect();
if (!(reduceQuery?.matches && !ignoreReducedMotion)) fill.style.transition = '';
}
};
// 初始化位置不触发开场过渡
apply(current, true);
return {
set(next) {
if (destroyed) return;
const skipAnim = Boolean(reduceQuery?.matches && !ignoreReducedMotion);
apply(next, skipAnim);
if (destroyed) return;
el.dispatchEvent(new CustomEvent('progresschange', { bubbles: true, detail: { value: current } }));
},
value() {
return current;
},
destroy() {
if (destroyed) return;
destroyed = true;
el.classList.remove('cpr-force');
el.style.removeProperty('--cpr-duration');
el.style.removeProperty('--cpr-easing');
fill.style.strokeDasharray = '';
fill.style.strokeDashoffset = '';
fill.style.transition = '';
if (prevRole === null) el.removeAttribute('role'); else el.setAttribute('role', prevRole);
if (prevMin === null) el.removeAttribute('aria-valuemin'); else el.setAttribute('aria-valuemin', prevMin);
if (prevMax === null) el.removeAttribute('aria-valuemax'); else el.setAttribute('aria-valuemax', prevMax);
if (prevNow === null) el.removeAttribute('aria-valuenow'); else el.setAttribute('aria-valuenow', prevNow);
if (valueEl && prevText !== null) valueEl.textContent = prevText;
},
};
}HTML
<div class="cpr-ring" id="ring" role="progressbar" aria-label="上传进度" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0">
<svg viewBox="0 0 96 96" aria-hidden="true">
<circle class="cpr-track" cx="48" cy="48" r="40" />
<circle class="cpr-fill" cx="48" cy="48" r="40" />
</svg>
<span class="cpr-value" data-cpr-value>0%</span>
</div>
<script type="module">
import { progressRing } from './progress-ring.js';
const ctl = progressRing(document.getElementById('ring'), { value: 0 });
// 随真实上传或任务进度调用 ctl.set(percent)
ctl.set(68);
</script>