vue 圆形百分比进度条_vue实用组件——圆环百分比进度条
有需要的人可以參考一下,如果試用過(guò),發(fā)現(xiàn)問(wèn)題,歡迎留言告知,不勝感激。
功能介紹:
1、若頁(yè)面無(wú)刷新,且第一次傳值小于第二次傳值或者圓環(huán)初始化時(shí)執(zhí)行的是遞增動(dòng)畫
2、若頁(yè)面無(wú)刷新,且第一次傳值大于第二次傳值則為執(zhí)行遞減動(dòng)畫
3、中間展示的百分比數(shù)字有緩動(dòng)動(dòng)畫(速度同圓環(huán)進(jìn)度動(dòng)畫一直)
4、動(dòng)畫完成時(shí)會(huì)觸發(fā)動(dòng)畫完成回調(diào)
5、外部傳值為圓環(huán)進(jìn)度百分比(整數(shù)),圓環(huán)動(dòng)畫速度(整數(shù))
效果如圖所示:
{{ percent }} %
export default {
props: {
percentNum: {
type: [String, Number],
default: 0
},
speed: { // 建議取值為0-3
type: [String, Number],
default: 1
}
},
data () {
return {
percent: 0,
initDeg: 0,
timeId: null,
animationing: false
}
},
methods: {
transformToDeg (percent) {
let deg = 0
if (percent >= 100) {
deg = 360
} else {
deg = parseInt(360 * percent / 100)
}
return deg
},
transformToPercent (deg) {
let percent = 0
if (deg >= 360) {
percent = 100
} else {
percent = parseInt(100 * deg / 360)
}
return percent
},
rotateLeft (deg) { // 大于180時(shí),執(zhí)行的動(dòng)畫
this.$refs.leftcontent.style.transform = 'rotate(' + (deg - 180) + 'deg)'
},
rotateRight (deg) { // 小于180時(shí),執(zhí)行的動(dòng)畫
this.$refs.rightcontent.style.transform = 'rotate(' + deg + 'deg)'
},
goRotate (deg) {
this.animationing = true
this.timeId = setInterval(() => {
if (deg > this.initDeg) { // 遞增動(dòng)畫
this.initDeg += Number(this.speed)
if (this.initDeg >= 180) {
this.rotateLeft(this.initDeg)
this.rotateRight(180) // 為避免前后兩次傳入的百分比轉(zhuǎn)換為度數(shù)后的值不為步距的整數(shù),可能出現(xiàn)的左右轉(zhuǎn)動(dòng)不到位的情況。
} else {
this.rotateRight(this.initDeg)
}
} else { // 遞減動(dòng)畫
this.initDeg -= Number(this.speed)
if (this.initDeg >= 180) {
this.rotateLeft(this.initDeg)
} else {
this.rotateLeft(180) // 為避免前后兩次傳入的百分比轉(zhuǎn)換為度數(shù)后的值不為步距的整數(shù),可能出現(xiàn)的左右轉(zhuǎn)動(dòng)不到位的情況。
this.rotateRight(this.initDeg)
}
}
this.percent = this.transformToPercent(this.initDeg) // 百分比數(shù)據(jù)滾動(dòng)動(dòng)畫
const remainer = Number(deg) - this.initDeg
if (Math.abs(remainer) < this.speed) {
this.initDeg += remainer
if (this.initDeg > 180) {
this.rotateLeft(deg)
} else {
this.rotateRight(deg)
}
this.animationFinished()
}
}, 10)
},
animationFinished () {
this.percent = this.percentNum // 百分比數(shù)據(jù)滾動(dòng)動(dòng)畫
this.animationing = false
clearInterval(this.timeId)
this.$emit('animationFinished') // 動(dòng)畫完成的回調(diào)
}
},
created () {
this.goRotate(this.transformToDeg(this.percentNum))
},
watch: {
'percentNum': function (val) {
if (this.animationing) return
this.goRotate(this.transformToDeg(val))
}
}
}
.percentloop {
position: relative;
width: 100%;
height: 100%;
border-radius: 50%;
overflow: hidden;
.circle-left, .circle-right {
position: absolute;
top: 0;
left: 0;
width: 50%;
height: 100%;
background-color: red;
overflow: hidden;
&>div {
width: 100%;
height: 100%;
background-color: #8a8a8a;
transform-origin: right center;
/*transition: all .5s linear;*/
}
}
.circle-right {
left: 50%;
&>div {
transform-origin: left center;
}
}
.number {
position: absolute;
top: 9%;
bottom: 9%;
left: 9%;
right: 9%;
background-color: #fff;
border-radius: 50%;
overflow: hidden;
display: flex;
align-items: center;
justify-content: center;
color: #000;
}
}
原文出處:https://www.cnblogs.com/qddyh/p/10386176.html
總結(jié)
以上是生活随笔為你收集整理的vue 圆形百分比进度条_vue实用组件——圆环百分比进度条的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: centos snmp配置_如何在Cen
- 下一篇: 5u fb库 三菱plc_三菱FX5U