
<template>
<div class="content" ref="box">
<svg
:id="idStr"
style="transform: rotate(-90deg)"
:width="width"
:height="width"
xmlns="http://www.w3.org/2000/svg"
>
<linearGradient :id="`gradient-${id}`" gradientUnits="userSpaceOnUse">
<stop
v-for="(item, index) in gradientsColor"
:key="index"
:offset="item.offset"
:stop-color="item.color"
/>
linearGradient>
<circle
:r="(width-radius)/2"
:cy="width/2"
:cx="width/2"
:stroke-width="radius"
:stroke="backgroundColor"
fill="none"
/>
<circle
v-if="gradientsColorShow"
ref="$bar"
:r="(width-radius)/2"
:cy="width/2"
:cx="width/2"
:stroke="gradientsColor ? `url(#gradient-${id})` : barColor"
:stroke-width="radius"
:stroke-linecap="isRound ? 'round' : 'square'"
:stroke-dasharray="(width-radius)*3.14"
:stroke-dashoffset="dashoffsetValue"
fill="none"
/>
svg>
<div class="center_text">
<p v-if="!$slots.default" class="title">{{progress}}%p>
<slot>slot>
div>
div>
template>
<script>
export default {
props: {
widthPresent: {
type: Number,
default: 1
},
gradientsColor: {
type: [Boolean, Array],
default: false
},
radius: {
type: [Number, String],
default: 20
},
progress: {
type: [Number, String],
default: 20
},
barColor: {
type: String,
default: "#1890ff"
},
backgroundColor: {
type: String,
default: "rgba(0,0,0,0.3)"
},
isAnimation: {
type: Boolean,
default: true
},
isRound: {
type: Boolean,
default: true
},
id: {
type: [String, Number],
default: 1
},
duration: {
type: [String, Number],
default: 1000
},
delay: {
type: [String, Number],
default: 200
},
timeFunction: {
type: String,
default: "cubic-bezier(0.99, 0.01, 0.22, 0.94)"
}
},
data() {
return {
width: 200,
idStr: "",
oldValue: 0
};
},
computed: {
gradientsColorShow: function() {
return true;
},
dashoffsetValue: function() {
const { isAnimation, width, radius, progress, oldValue } = this;
return isAnimation
? ((width - radius) * 3.14 * (100 - oldValue)) / 100
: ((width - radius) * 3.14 * (100 - progress)) / 100;
}
},
watch: {
id: function() {
this.idStr = `circle_progress_keyframes_${this.id || 1}`;
},
progress: function(newData, oldData) {
if (newData !== oldData) {
this.oldValue = oldData;
this.setAnimation();
}
}
},
mounted() {
this.idStr = `circle_progress_keyframes_${this.id || 1}`;
let self = this;
this.setCircleWidth();
this.setAnimation();
window.addEventListener("resize", function() {
self.setCircleWidth();
self.setAnimation(self);
});
},
methods: {
setCircleWidth() {
const { widthPresent } = this;
this.$nextTick(() => {
let box = this.$refs.box;
let width = box.clientWidth * widthPresent;
let height = box.clientHeight * widthPresent;
let cW = width > height ? height : width;
this.width = cW;
})
},
setAnimation() {
let self = this;
if (self.isAnimation) {
if (document.getElementById(self.idStr)) {
self.$refs.$bar.classList.remove(`circle_progress_bar${self.id}`);
}
this.$nextTick(() => {
this.startAnimation();
});
}
},
startAnimation() {
let style = document.createElement("style");
style.id = this.idStr;
style.type = "text/css";
style.innerHTML = `
@keyframes circle_progress_keyframes_name_${this.id} {
from {stroke-dashoffset: ${((this.width - this.radius) *
3.14 *
(100 - this.oldValue)) /
100}px;}
to {stroke-dashoffset: ${((this.width - this.radius) *
3.14 *
(100 - this.progress)) /
100}px;}}
.circle_progress_bar${
this.id
} {animation: circle_progress_keyframes_name_${this.id} ${
this.duration
}ms ${this.delay}ms ${this.timeFunction} forwards;}`;
document.getElementsByTagName("head")[0].appendChild(style);
this.$refs.$bar.classList.add(`circle_progress_bar${this.id}`);
}
}
};
script>
<style scoped>
.content {
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.center_text {
position: absolute;
font-size: 16px;
font-weight: bold;
}
style>

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209