效果图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/22994e6fa4d463727126ebbce2f9b3e1.gif)
本次进度条运用到svg 和 circle标签 对此方面不太熟悉的小伙伴可以收藏记录 防止用的时候找不到 因多为标签运用及css 少量js代码 方便理解每个参数的作用 注释都在代码中
HTML
<div>
<svg width='200' height='200' viewBox="0 0 60 60" style="width:200px;height:200px;">
<circle cx="30" cy="30" r="27" fill="transparent" stroke-width="4" stroke="#eee" />
<circle class="progress" r="27" cy="30" cx="30" stroke-width="4" stroke="blue"
stroke-linejoin="round" stroke-linecap="round" fill="none" stroke-dashoffset="0px"
stroke-dasharray="169px" />
</svg>
</div>
<style>
svg {
transform: rotate(-90deg);
}
.progress {
animation: rotate 1s linear both;
}
@keyframes rotate {
from {
stroke-dashoffset: 169px;
}
to {
stroke-dashoffset: 0px;
}
}
</style>
数据可控圆环进度
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d583210d996bd02a473634c06db4c1e0.gif)
js控制显示进度
<div>
<svg width='200' height='200' viewBox="0 0 60 60" style="width:200px;height:200px;">
<circle cx="30" cy="30" r="27" fill="transparent" stroke-width="4" stroke="#eee" />
<circle class="progress" id="progress-bar" r="27" cy="30" cx="30" stroke-width="4" stroke="blue" stroke-linejoin="round"
stroke-linecap="round" fill="none" stroke-dashoffset="0px" stroke-dasharray="169px" />
</svg>
<span id="progress-detail">20%</span>
<button onclick="reduce()">减少</button>
<button onclick="add()">增加</button>
</div>
<script>
const bar = document.getElementById('progress-bar')
const detail = document.getElementById('progress-detail')
const total = 169
const per = total / 100
let progress = 20
update();
function add() {
if (progress >= 100) {
return
}
progress += 20
update()
}
function reduce() {
if (progress <= 0) {
return
}
progress -= 20
update()
}
function update() {
bar.style.strokeDashoffset = (total - per * progress);
detail.innerHTML = `${progress}%`
}
</script>