-
Notifications
You must be signed in to change notification settings - Fork 0
/
promiseLoading.html
384 lines (332 loc) · 10.3 KB
/
promiseLoading.html
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>promise</title>
</head>
<style>
.mask {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
background-color: rgba(0, 0, 0, 0.5);
}
.snake-loading {
z-index: 99999;
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
background-color: rgba(3, 3, 3, 0.3);
}
.snake-loading.fixed {
position: fixed;
}
.sk-fading-circle {
margin: 40px auto;
width: 40px;
height: 40px;
position: relative;
}
.sk-fading-circle .sk-circle {
width: 100%;
height: 100%;
position: absolute;
left: 0;
top: 0;
}
.sk-fading-circle .sk-circle:before {
content: '';
display: block;
margin: 0 auto;
width: 15%;
height: 15%;
background-color: #333;
border-radius: 100%;
-webkit-animation: sk-circleFadeDelay 1.2s infinite ease-in-out both;
animation: sk-circleFadeDelay 1.2s infinite ease-in-out both;
}
.sk-fading-circle .sk-circle2 {
-webkit-transform: rotate(30deg);
-ms-transform: rotate(30deg);
transform: rotate(30deg);
}
.sk-fading-circle .sk-circle3 {
-webkit-transform: rotate(60deg);
-ms-transform: rotate(60deg);
transform: rotate(60deg);
}
.sk-fading-circle .sk-circle4 {
-webkit-transform: rotate(90deg);
-ms-transform: rotate(90deg);
transform: rotate(90deg);
}
.sk-fading-circle .sk-circle5 {
-webkit-transform: rotate(120deg);
-ms-transform: rotate(120deg);
transform: rotate(120deg);
}
.sk-fading-circle .sk-circle6 {
-webkit-transform: rotate(150deg);
-ms-transform: rotate(150deg);
transform: rotate(150deg);
}
.sk-fading-circle .sk-circle7 {
-webkit-transform: rotate(180deg);
-ms-transform: rotate(180deg);
transform: rotate(180deg);
}
.sk-fading-circle .sk-circle8 {
-webkit-transform: rotate(210deg);
-ms-transform: rotate(210deg);
transform: rotate(210deg);
}
.sk-fading-circle .sk-circle9 {
-webkit-transform: rotate(240deg);
-ms-transform: rotate(240deg);
transform: rotate(240deg);
}
.sk-fading-circle .sk-circle10 {
-webkit-transform: rotate(270deg);
-ms-transform: rotate(270deg);
transform: rotate(270deg);
}
.sk-fading-circle .sk-circle11 {
-webkit-transform: rotate(300deg);
-ms-transform: rotate(300deg);
transform: rotate(300deg);
}
.sk-fading-circle .sk-circle12 {
-webkit-transform: rotate(330deg);
-ms-transform: rotate(330deg);
transform: rotate(330deg);
}
.sk-fading-circle .sk-circle2:before {
-webkit-animation-delay: -1.1s;
animation-delay: -1.1s;
}
.sk-fading-circle .sk-circle3:before {
-webkit-animation-delay: -1s;
animation-delay: -1s;
}
.sk-fading-circle .sk-circle4:before {
-webkit-animation-delay: -0.9s;
animation-delay: -0.9s;
}
.sk-fading-circle .sk-circle5:before {
-webkit-animation-delay: -0.8s;
animation-delay: -0.8s;
}
.sk-fading-circle .sk-circle6:before {
-webkit-animation-delay: -0.7s;
animation-delay: -0.7s;
}
.sk-fading-circle .sk-circle7:before {
-webkit-animation-delay: -0.6s;
animation-delay: -0.6s;
}
.sk-fading-circle .sk-circle8:before {
-webkit-animation-delay: -0.5s;
animation-delay: -0.5s;
}
.sk-fading-circle .sk-circle9:before {
-webkit-animation-delay: -0.4s;
animation-delay: -0.4s;
}
.sk-fading-circle .sk-circle10:before {
-webkit-animation-delay: -0.3s;
animation-delay: -0.3s;
}
.sk-fading-circle .sk-circle11:before {
-webkit-animation-delay: -0.2s;
animation-delay: -0.2s;
}
.sk-fading-circle .sk-circle12:before {
-webkit-animation-delay: -0.1s;
animation-delay: -0.1s;
}
@-webkit-keyframes sk-circleFadeDelay {
0%,
39%,
100% {
opacity: 0;
}
40% {
opacity: 1;
}
}
@keyframes sk-circleFadeDelay {
0%,
39%,
100% {
opacity: 0;
}
40% {
opacity: 1;
}
}
</style>
<body>
<div id="table" style="width: 200px;height: 200px;display: inline-block;border: 1px solid #ddd;">
<table>
<tr>
<td>123</td>
</tr>
</table>
<!-- <div class="mask">
<div class="sk-fading-circle">
<div class="sk-circle1 sk-circle"></div>
<div class="sk-circle2 sk-circle"></div>
<div class="sk-circle3 sk-circle"></div>
<div class="sk-circle4 sk-circle"></div>
<div class="sk-circle5 sk-circle"></div>
<div class="sk-circle6 sk-circle"></div>
<div class="sk-circle7 sk-circle"></div>
<div class="sk-circle8 sk-circle"></div>
<div class="sk-circle9 sk-circle"></div>
<div class="sk-circle10 sk-circle"></div>
<div class="sk-circle11 sk-circle"></div>
<div class="sk-circle12 sk-circle"></div>
</div>
</div> -->
</div>
<div id="table2" style="width: 200px;height: 200px;display: inline-block;border: 1px solid #ddd; margin-top: 20px;">
<table>
<tr>
<td>123</td>
</tr>
</table>
</div>
<div>
<button id="button">loading</button>
<button id="button2">loading2</button>
<button id="button3">loading3</button>
</div>
<script src="plugins/jQuery/jquery-2.1.4.js"></script>
<script>
(function($) {
// 当前文件路径
var BASE_PATH = "";
var CSS_PATH = "";
function Loading(promise, element, options) {
var self = this;
self.option = $.extend(true, {}, options);
self.$ele = $(element);
self.promise = promise;
self.show();
}
Loading.DEFAULT = {
maskEle: '<div id="snake-loading" class="snake-loading">' + '<div class="sk-fading-circle">' + '<div class="sk-circle1 sk-circle"></div>' + '<div class="sk-circle2 sk-circle"></div>' + '<div class="sk-circle3 sk-circle"></div>' + '<div class="sk-circle4 sk-circle"></div>' + '<div class="sk-circle5 sk-circle"></div>' + '<div class="sk-circle6 sk-circle"></div>' + '<div class="sk-circle7 sk-circle"></div>' + '<div class="sk-circle8 sk-circle"></div>' + '<div class="sk-circle9 sk-circle"></div>' + '<div class="sk-circle10 sk-circle"></div>' + '<div class="sk-circle11 sk-circle"></div>' + '<div class="sk-circle12 sk-circle"></div>' + '</div>' + '</div>'
};
Loading.prototype.show = function() {
var self = this;
self.$ele.append(self.option.maskEle);
self.$ele.css("position", "relative");
$.when(self.promise).then(
function() {
self.hide();
}
).done(function() {
self.hide();
})
.fail(function() {
self.hide();
});
};
Loading.prototype.hide = function(element) {
var self = this;
if (element) {
$(element).children('#snake-loading').remove();
element.css("position", "");
} else {
self.$ele.children('#snake-loading').remove();
self.$ele.css("position", "");
}
};
function Plugin(promise, options) {
var option = $.extend({}, $.extend(Loading.DEFAULT), typeof options == "object" && options);
if (this.height() < window.screen.availHeight) {
option.maskEle = option.maskEle.replace("sk-fading-circle", ("sk-fading-circle center"));
}
if (this[0].tagName == "BODY") {
option.maskEle = option.maskEle.replace('class="snake-loading"', ('class="snake-loading fixed"'));
}
return this.each(function() {
var $this = $(this);
var data = $(this).data("snake.loading");
var options = option;
$this.data("snake.loading", (data = new Loading(promise, this, options)))
if (typeof promise == "hide") {
data.hide(option);
}
});
}
_addStyle = function() {
var style = document.createElement('link'); //创建一个style元素
var head = document.head || document.getElementsByTagName('head')[0]; //获取head元素
style.rel = 'stylesheet'; //这里必须显示设置style元素的type属性为text/css,否则在ie中不起作用
style.setAttribute("href", CSS_PATH);
head.appendChild(style); //把创建的style元素插入到head中
};
function _getCssPath() {
$(document.scripts).each(function(index, el) {
if (el.src.indexOf("loading-0.1.0.js") > -1) {
BASE_PATH = el.src;
CSS_PATH = el.src.replace("loading-0.1.0.js", "loading.css");
}
});
}
function _init() {
// _getCssPath();
// _addStyle();
}
_init();
var old = $.fn.loading;
jQuery.loading = $.fn.loading = Plugin;
// DROPDOWN NO CONFLICT
// ====================
$.fn.loading.noConflict = function() {
$.fn.loading = old;
return this
}
})(jQuery);
</script>
<script>
(function($) {
//模拟请求
function ajax() {
var dtd = $.Deferred();
setTimeout(dtd.resolve, 2000);
// setTimeout(dtd.reject, 2000);
// setTimeout(dtd.notify, 2000);
return dtd.promise(); //此处也可以直接返回dtd
};
function ajax2() {
let promise = Promise();
setTimeout(promise.resolve, 2000);
return promise;
}
$("#button").on("click", function() {
//loading
var loading = ajax();
// $.loading(loading, $("#table"));
$("#table").loading(loading);
})
$("#button2").on("click", function() {
//loading
// var loading = ajax();
var loading = ajax2();
$("#table2").loading(loading);
})
$("#button3").on("click", function() {
//loading
var loading = ajax();
$("body").loading(loading);
})
})(jQuery)
</script>
</body>
</html>