-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathvue-loading-bar.vue
192 lines (151 loc) · 3.43 KB
/
vue-loading-bar.vue
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
<style>
.loading-bar{
transition:all 0.3s ease;
-moz-transition:all 0.3s ease;
-webkit-transition:all 0.3s ease;
-o-transition:all 0.3s ease;
}
.loading-bar{
position: fixed;
top: 0;
background: #1AF184;
height: 3px;
opacity: 1;
}
.glow-bar{
top: 0;
position: absolute;
width: 100px;
height: 100%;
box-shadow: -3px 0 15px 1px rgba(0, 255, 231, 0.4)
}
.to-right.loading-bar{
left: 0;
}
.to-right .glow-bar{
right: 0;
}
.to-left.loading-bar{
right: 0;
}
.to-left .glow-bar{
left: 0;
}
.full.loading-bar{
transition:all 0.1s ease;
-moz-transition:all 0.1s ease;
-webkit-transition:all 0.1s ease;
-o-transition:all 0.1s ease;
height: 0;
opacity: 0;
}
.error.loading-bar{
background: #F44336;
}
.error .glow-bar{
box-shadow: -3px 0 15px 1px rgba(244, 67, 54, 0.4);
}
</style>
<template>
<div v-if="show"
class="loading-bar to-{{ direction }} {{ full }} {{ class != undefined ? class : '' }} {{ error ? 'error' : '' }}"
:id="id != undefined ? 'loading-bar-'+id : ''"
:style="styling()">
<div class="glow-bar"></div>
</div>
</template>
<script>
/*! Copyright (c) 2016 Naufal Rabbani (http://github.com/BosNaufal)
* Licensed Under MIT (http://opensource.org/licenses/MIT)
*
* Version 0.0.1
*
*/
export default {
props: {
id: String,
class: String,
progress: Number, // The progress of loading bar
// the direction of loading bar
direction: {
type: String,
default: "right"
},
error: Boolean // Loading Bar error state
},
data() {
return {
// To show
show: true,
// binding class when it end
full: '',
// state to animate the width of loading bar
width: 0,
// indicate the loading bar is in 100% ( so, wait it till gone )
wait: false
};
},
watch:{
progress(val,old){
if(old == 0 && val > 0){
// Callback Event when it's started
this.$dispatch('loading-bar:started');
}
if(val > 1 && val < 100){
// Callback Event when it's loading
this.$dispatch('loading-bar:loading');
}
// When the progress end
if(this.progress == 100){
// Prevent new progress change
this.wait = true;
// Start animate it
this.width = 100;
setTimeout(() => {
// animate when element removed
this.full = 'full';
this.error = false;
setTimeout(() => {
//remove bar element
this.show = false;
// New Element is available to create now
this.progress = 0;
this.wait = false;
setTimeout(() => {
// Show Bar
this.full = '';
this.show = true;
// Callback Event when it's loaded and totally gone
this.$dispatch('loading-bar:loaded');
});
// Duration to Waiting for slick hiding animation
},250);
// Duration is depend on css animation-duration of loading-bar
},700);
// When the progress doesn't end yet
}else{
// Do normaly loading bar animation
this.full = '';
this.width = val;
}
},
error(val,old){
this.progress = 100;
// Callback Event when it's error
this.$dispatch('loading-bar:error');
}
},
methods: {
styling(){
// When loading bar still in progress
if(!this.wait){
return { width: `${this.width}%` };
// When loading bar end
}else{
// Make it stuck at 100 to waiting the animation
return{ width: `100%` };
}
}
}
}
</script>