在做项目的时候,最好只使用一套组件库,但是很多时候我们的组件库里面没有我们需要的组件,这个时候我们还是需要自己写组件了,vux里面就没有环形进度条组件,所以需要自己写一个。
查找资料后发现了一个很好的实现方式,通过svg来实现,以前的时候学过一点svg但是没有怎么深入了解过。。。现在看来真是罪过,给出参考链接
https://segmentfault.com/a/1190000008149403
可以看出原作者使用了两种方式,我们选择了第二种,简单,而且好扩展。可以看到svg就想是canvas一样进行绘图。原文已经讲得很详细了,这里就附上自己写的组件吧。伸手党们也能愉快一点。
<template>
<svg :height="option.size" :width="option.size" x-mlns="http://www.w3.org/200/svg">
<circle
:r="option.radius"
:cx="option.cx"
:cy="option.cy"
:stroke="option.outerColor"
:stroke-width="option.strokeWidth"
fill="none"
stroke-linecap="round"/>
<circle
id="progressRound"
:stroke-dasharray="completenessHandle"
:r="option.radius"
:cx="option.cx"
:cy="option.cy"
:stroke-width="option.strokeWidth"
:stroke="option.innerColor"
fill="none"
class="progressRound"
/>
</svg>
</template>
< >
export default {
name: 'CommonLoopProgress',
props: {
completeness: {
type: Number,
required: true,
},
progressOption: {
type: ,
default: () => {},
},
},
data () {
return {
}
},
computed: {
completenessHandle () {
let circleLength = Math.floor(2 * Math.PI * this.option.radius)
let completenessLength = this.completeness * circleLength
return `${completenessLength},100000000`
},
option () {
// 所有进度条的可配置项
let Option = {
radius: 20,
strokeWidth: 5,
outerColor: '#E6E6E6',
innerColor: '#FFDE00',
}
.assign( Option, this.progressOption)
// 中心位置自动生成
Option.cy = Option.cx = Option.radius + Option.strokeWidth
Option.size = ( Option.radius + Option.strokeWidth) * 2
return Option
},
},
}
</ >
<style scoped lang='stylus'>
@import '~stylus/_variables.styl';
@import '~stylus/_mixins.styl';
.progressRound {
transform-origin: center;
transform: rotate(-90deg);
transition: stroke-dasharray 0.3s ease-in;
}
</style>
修改了原文中的一些不好的命名方式,并且让我们的组件方便配置,可以自由一点。
以上就是本次知识点的全部内容,感谢大家对脚本之家的支持。
继续阅读与本文标签相同的文章
下一篇 :
Hulu慎待区块链
-
韩国公布500亿美元计划 大力发展电动和自动驾驶汽车
2026-05-18栏目: 教程
-
原来这样做可以提高自媒体短视频的播放量?
2026-05-18栏目: 教程
-
用了3年以上的iPhone手机,应该这样清理手机缓存,很实用
2026-05-18栏目: 教程
-
一文弄懂,锁的基本概念到Redis分布式锁实现
2026-05-18栏目: 教程
-
阿里云混合云备份如何还原虚拟机备份?
2026-05-18栏目: 教程
