Skip to content

Commit 57abafb

Browse files
committed
feat: add circle progress
1 parent 2b36531 commit 57abafb

File tree

3 files changed

+148
-4
lines changed

3 files changed

+148
-4
lines changed

doc/markdown/nProgress/en-US/index.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ When an operation takes a long time to complete, the user is shown the current p
66

77

88
## Examples
9+
### Basic Use
910
:::demo
1011
```html
1112
<template>
@@ -18,10 +19,21 @@ When an operation takes a long time to complete, the user is shown the current p
1819
```
1920
:::
2021

22+
### Circular progress bar
23+
:::demo
24+
```html
25+
<template>
26+
<n-progress type="circle" width="100" stroke-width="10" color="rgb(52, 152, 255)" :percent="50"/>
27+
</template>
28+
29+
```
30+
:::
31+
2132
### API
2233

2334
| Property | Description | Type | Default |
2435
| :--- | :--- | :--- | :--- |
2536
| color | Line color | String | |
2637
| percent | Progress percentage | Number | 0 |
27-
| width | Progress bar width | Number | 10 |
38+
| width | Progress bar width,when type is circle,it means canvas width | Number | 10 |
39+
| stroke-width | When type is circle,it means the width of the circle | Number | 10 |

doc/markdown/nProgress/zh-CN/index.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
在操作需要较长时间才能完成时,为用户显示该操作的当前进度和状态。
66

77
## 代码演示
8+
9+
### 基本使用
810
:::demo
911
```html
1012
<template>
@@ -16,10 +18,23 @@
1618
```
1719
:::
1820

21+
### 进度圈
22+
:::demo
23+
```html
24+
<template>
25+
<n-progress type="circle" width="100" stroke-width="10" color="rgb(52, 152, 255)" :percent="50"/>
26+
</template>
27+
28+
```
29+
:::
30+
1931
## API
2032

2133
| 参数 | 说明 | 类型 | 默认值 |
2234
| :--- | :--- | :--- | :--- |
35+
| type | 进度条形状,可选`line`,`circle` | String | `line` |
2336
| color | 进度条颜色 | String | |
2437
| percent | 进度百分比 | Number | 0 |
25-
| width | 进度条宽度(像素) | Number | 10 |
38+
| width | 进度条宽度(像素),type为circle时,代表画布宽度 | Number | 10 |
39+
| stroke-width | type为circle时使用,代表圆圈的宽度 | Number | 10 |
40+

packages/nProgress/src/index.vue

Lines changed: 119 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<template>
22
<div class="n-progress">
3-
<div class="n-progress-line">
3+
<div
4+
v-if="type === 'line'"
5+
class="n-progress-line"
6+
>
47
<div
58
class="n-progress-line-outer"
69
:style="{height: `${width}px`}"
@@ -18,13 +21,51 @@
1821
{{ progressWidth }}
1922
</div>
2023
</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>
2158
</div>
2259
</template>
2360

2461
<script>
2562
export default {
2663
name: 'NProgress',
2764
props: {
65+
type: {
66+
type: String,
67+
default: 'line'
68+
},
2869
percent: {
2970
type: Number,
3071
default: 0
@@ -33,15 +74,73 @@ export default {
3374
type: String,
3475
default: '#e5e5ea'
3576
},
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+
},
36105
width: {
37-
type: Number,
106+
type: [Number, String],
107+
default: 10
108+
},
109+
strokeWidth: {
110+
type: [Number, String],
38111
default: 10
39112
}
113+
114+
},
115+
data () {
116+
return {
117+
idStr: `circle_progress_keyframes_${Date.parse(new Date())}`
118+
};
40119
},
41120
computed: {
42121
progressWidth () {
43122
return `${this.percent}%`;
44123
}
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+
}
45144
}
46145
};
47146
</script>
@@ -77,5 +176,23 @@ export default {
77176
color:#575757
78177
}
79178
}
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+
}
80197
}
81198
</style>

0 commit comments

Comments
 (0)