使用svg和circle来实现进度条。
circle标签的属性:
- cx:圆心的x座标
- cy:圆心的y座标
- r:圆的半径
- fill:填充的颜色
- stroke:边框的填充的颜色
- stroke-width:边框的大小
- stroke-dasharray:圆的周长2PIR
- stroke-dashoffset:等于周长就是边框空白,等于0就填充完边框
实现原理:画出两个圆,两个园的边框填充颜色不一样,填充第一个园的边框,动态改变第二个元的stroke-dashoffset的值,让它从圆的周长变到0,然后填充完整个边框。
全部代码:
<!DOCTYPE html>
<html lang="en">
<head>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>SVG实现圆形进度条</title>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<style>
#app {
width: 300px;
height: 300px;
margin: 0 auto;
margin-top: 300px;
}
svg {
width: 400px;
height: 400px;
}
svg circle {
transform-origin: 100px 100px;
transform: rotate(-90deg)
}
</style>
</head>
</head>
<body>
<div id="app">
<svg ref="svg">
<circle cx="100" cy="100" r="50" fill="transparent" stroke="#c09d31" stroke-width="5" stroke-dasharray="314"
stroke-dashoffset="0"></circle>
<circle cx="100" cy="100" r="50" fill="transparent" stroke="#ffcd32" stroke-width="5" stroke-dasharray="314"
:stroke-dashoffset="offset">
</circle>
</svg>
</div>
</body>
<script>
new Vue({
el: '#app',
data: {
offset: 314
},
mounted() {
this.interval = setInterval(() => {
this.offset--
}, 100)
},
watch: {
offset(newval) {
if (newval <= 0) {
window.clearInterval(this.interval)
}
}
}
})
</script>
</html>