1
1
<template >
2
2
<div class =" n-progress" >
3
- <div class =" n-progress-line" >
3
+ <div
4
+ v-if =" type === 'line'"
5
+ class =" n-progress-line"
6
+ >
4
7
<div
5
8
class =" n-progress-line-outer"
6
9
:style =" {height: `${width}px`}"
18
21
{{ progressWidth }}
19
22
</div >
20
23
</div >
24
+ <div
25
+ v-else
26
+ class =" n-progress-circle"
27
+ :style =" {width: `${width}px`}"
28
+ >
29
+ <svg
30
+ style =" transform : rotate (-90deg )"
31
+ :width =" width"
32
+ :height =" width"
33
+ xmlns =" http://www.w3.org/2000/svg"
34
+ >
35
+ <circle
36
+ :r =" (width-strokeWidth)/2"
37
+ :cy =" width/2"
38
+ :cx =" width/2"
39
+ :stroke-width =" strokeWidth"
40
+ :stroke =" backgroundColor"
41
+ fill =" none"
42
+ />
43
+ <circle
44
+ ref =" $bar"
45
+ :r =" (width-strokeWidth)/2"
46
+ :cy =" width/2"
47
+ :cx =" width/2"
48
+ :stroke =" color"
49
+ :stroke-width =" strokeWidth"
50
+ :stroke-linecap =" isRound ? 'round' : 'square'"
51
+ :stroke-dasharray =" (width-strokeWidth)*3.14"
52
+ :stroke-dashoffset =" (width - strokeWidth) * 3.14 * (100 - percent) / 100"
53
+ fill =" none"
54
+ />
55
+ </svg >
56
+ <span >{{ percent + `%` }}</span >
57
+ </div >
21
58
</div >
22
59
</template >
23
60
24
61
<script >
25
62
export default {
26
63
name: ' NProgress' ,
27
64
props: {
65
+ type: {
66
+ type: String ,
67
+ default: ' line'
68
+ },
28
69
percent: {
29
70
type: Number ,
30
71
default: 0
@@ -33,15 +74,73 @@ export default {
33
74
type: String ,
34
75
default: ' #e5e5ea'
35
76
},
77
+ backgroundColor: {
78
+ type: String ,
79
+ default: ' #e5e5ea'
80
+ },
81
+ isAnimation: {
82
+ type: Boolean ,
83
+ default: true
84
+ },
85
+ isRound: {
86
+ type: Boolean ,
87
+ default: true
88
+ },
89
+ id: {
90
+ type: [String , Number ],
91
+ default: 1
92
+ },
93
+ duration: {
94
+ type: [String , Number ],
95
+ default: 1000
96
+ },
97
+ delay: {
98
+ type: [String , Number ],
99
+ default: 200
100
+ },
101
+ timeFunction: {
102
+ type: String ,
103
+ default: ' cubic-bezier(0.99, 0.01, 0.22, 0.94)'
104
+ },
36
105
width: {
37
- type: Number ,
106
+ type: [Number , String ],
107
+ default: 10
108
+ },
109
+ strokeWidth: {
110
+ type: [Number , String ],
38
111
default: 10
39
112
}
113
+
114
+ },
115
+ data () {
116
+ return {
117
+ idStr: ` circle_progress_keyframes_${ Date .parse (new Date ())} `
118
+ };
40
119
},
41
120
computed: {
42
121
progressWidth () {
43
122
return ` ${ this .percent } %` ;
44
123
}
124
+ },
125
+ mounted () {
126
+ if (this .type === ' circle' && this .isAnimation ) {
127
+ if (document .getElementById (this .idStr )) {
128
+ console .warn (' vue-circle-progress should not have same id style' );
129
+ document .getElementById (this .idStr ).remove ();
130
+ }
131
+
132
+ let style = document .createElement (' style' );
133
+ style .id = this .idStr ;
134
+ style .type = ' text/css' ;
135
+ style .innerHTML = `
136
+ @keyframes circle_progress_keyframes_name_${ this .idStr } {
137
+ from {stroke-dashoffset: ${ (this .width - this .radius ) * 3.14 } px;}
138
+ to {stroke-dashoffset: ${ (this .width - this .radius ) * 3.14 * (100 - this .progress ) / 100 } px;}}
139
+ .circle_progress_bar${ this .idStr } {animation: circle_progress_keyframes_name_${ this .idStr } ${ this .duration } ms ${ this .delay } ms ${ this .timeFunction } forwards;}
140
+ ` ;
141
+ document .getElementsByTagName (' head' )[0 ].appendChild (style);
142
+ this .$refs .$bar .classList .add (` circle_progress_bar${ this .idStr } ` );
143
+ }
45
144
}
46
145
};
47
146
</script >
@@ -77,5 +176,23 @@ export default {
77
176
color :#575757
78
177
}
79
178
}
179
+ .n-progress-circle {
180
+ position :relative ;
181
+ span {
182
+ position : absolute ;
183
+ top : 50% ;
184
+ left : 50% ;
185
+ width : 100% ;
186
+ margin : 0 ;
187
+ padding : 0 ;
188
+ color : rgba (0 ,0 ,0 ,.65 );
189
+ font-size : 1em ;
190
+ line-height : 1 ;
191
+ white-space : normal ;
192
+ text-align : center ;
193
+ -webkit-transform : translate (-50% ,-50% );
194
+ transform : translate (-50% ,-50% );
195
+ }
196
+ }
80
197
}
81
198
</style >
0 commit comments