|
1 | 1 | <template> |
2 | | - <p>LOADING...</p> |
| 2 | + <div class="progress" :style="{ |
| 3 | + 'width': percent+'%', |
| 4 | + 'height': height, |
| 5 | + 'background-color': canSuccess? color : failedColor, |
| 6 | + 'opacity': show ? 1 : 0 |
| 7 | + }"></div> |
3 | 8 | </template> |
4 | 9 |
|
5 | 10 | <script> |
| 11 | +const Vue = require('vue') |
| 12 | +
|
6 | 13 | export default { |
7 | 14 | data () { |
8 | 15 | return { |
| 16 | + percent: 0, |
| 17 | + show: false, |
| 18 | + canSuccess: true, |
9 | 19 | duration: <%= loading.duration %>, |
10 | | - loadingColor: '<%= loading.loadingColor %>', |
11 | | - errorColor: '<%= loading.errorColor %>', |
| 20 | + height: '<%= loading.height %>', |
| 21 | + color: '<%= loading.color %>', |
| 22 | + failedColor: '<%= loading.failedColor %>', |
12 | 23 | } |
13 | 24 | }, |
14 | 25 | methods: { |
15 | | - start () {}, |
16 | | - increase () {}, |
17 | | - decrease () {}, |
18 | | - set () {}, |
19 | | - finish () {}, |
20 | | - pause () {}, |
21 | | - hide () {}, |
22 | | - fail () {} |
| 26 | + start () { |
| 27 | + this.show = true |
| 28 | + this.canSuccess = true |
| 29 | + if (this._timer) { |
| 30 | + clearInterval(this._timer) |
| 31 | + this.percent = 0 |
| 32 | + } |
| 33 | + this._cut = 10000 / Math.floor(this.duration) |
| 34 | + this._timer = setInterval(() => { |
| 35 | + this.increase(this._cut * Math.random()) |
| 36 | + if (this.percent > 95) { |
| 37 | + this.finish() |
| 38 | + } |
| 39 | + }, 100) |
| 40 | + return this |
| 41 | + }, |
| 42 | + set (num) { |
| 43 | + this.show = true |
| 44 | + this.canSuccess = true |
| 45 | + this.percent = Math.floor(num) |
| 46 | + return this |
| 47 | + }, |
| 48 | + get () { |
| 49 | + return Math.floor(this.percent) |
| 50 | + }, |
| 51 | + increase (num) { |
| 52 | + this.percent = this.percent + Math.floor(num) |
| 53 | + return this |
| 54 | + }, |
| 55 | + decrease () { |
| 56 | + this.percent = this.percent - Math.floor(num) |
| 57 | + return this |
| 58 | + }, |
| 59 | + finish () { |
| 60 | + this.percent = 100 |
| 61 | + this.hide() |
| 62 | + return this |
| 63 | + }, |
| 64 | + pause () { |
| 65 | + clearInterval(this._timer) |
| 66 | + return this |
| 67 | + }, |
| 68 | + hide () { |
| 69 | + clearInterval(this._timer) |
| 70 | + this._timer = null |
| 71 | + setTimeout(() => { |
| 72 | + this.show = false |
| 73 | + Vue.nextTick(() => { |
| 74 | + setTimeout(() => { |
| 75 | + this.percent = 0 |
| 76 | + }, 100) |
| 77 | + }) |
| 78 | + }, 800) |
| 79 | + return this |
| 80 | + }, |
| 81 | + fail () { |
| 82 | + this.canSuccess = false |
| 83 | + this.percent = 100 |
| 84 | + this.hide() |
| 85 | + return this |
| 86 | + } |
23 | 87 | } |
24 | 88 | } |
25 | 89 | </script> |
26 | 90 |
|
27 | 91 | <style scoped> |
28 | | - p { |
29 | | - color: grey; |
30 | | - } |
| 92 | +.progress { |
| 93 | + position: fixed; |
| 94 | + top: 0px; |
| 95 | + left: 0px; |
| 96 | + right: 0px; |
| 97 | + height: 2px; |
| 98 | + width: 0%; |
| 99 | + transition: width 0.2s, opacity 0.6s; |
| 100 | + opacity: 1; |
| 101 | + background-color: #efc14e; |
| 102 | + z-index: 999999; |
| 103 | +} |
31 | 104 | </style> |
0 commit comments