forked from lispc/covid19-citymap-china
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathloading.js
More file actions
97 lines (97 loc) · 2.55 KB
/
loading.js
File metadata and controls
97 lines (97 loc) · 2.55 KB
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
/*
* Created with Sublime Text 3.
* license: http://www.lovewebgames.com/jsmodule/index.html
* github: https://github.com/tianxiangbing/loading
* User: 田想兵
* Date: 2015-08-05
* Time: 11:27:55
* Contact: 55342775@qq.com
* desc:请尽量使用github上的代码,会修复一些问题,关注https://github.com/tianxiangbing/loading
*/
;
(function (root, factory) {
//amd
if (typeof define === 'function' && define.amd) {
define(['jquery'], factory);
} else if (typeof exports === 'object') { //umd
module.exports = factory($);
} else {
root.Loading = factory(window.Zepto || window.jQuery || $);
}
})(window, function ($) {
var Loading = function () { };
Loading.prototype = {
loadingTpl: '<div class="ui-loading"><div class="ui-loading-mask"></div><i></i></div>',
stop: function () {
this.loading.remove();
this.loading = null;
},
start: function () {
var _this = this;
var loading = this.loading;
if (!loading) {
loading = $(_this.loadingTpl);
$('body').append(loading);
}
this.loading = loading;
//console.log(cw,ch)
this.setPosition();
},
setPosition: function () {
var _this = this;
var loading = this.loading;
var target = _this.target;
var content = $(target);
var ch = $(content).outerHeight();
var cw = $(content).outerWidth();
if ($(target)[0].tagName == "HTML") {
ch = Math.max($(target).height(), $(window).height());
cw = Math.max($(target).width(), $(window).width());
}
loading.height(ch).width(cw);
loading.find('div').height(ch).width(cw);
if (ch < 100) {
loading.find('i').height(ch).width(ch);
}
var offset = $(content).offset();
loading.css({
top: offset.top,
left: offset.left
});
var icon = loading.find('i');
var h = ch,
w = cw,
top = 0,
left = 0;
if ($(target)[0].tagName == "HTML") {
h = $(window).height();
w = $(window).width();
top = (h - icon.height()) / 2 + $(window).scrollTop();
left = (w - icon.width()) / 2 + $(window).scrollLeft();
} else {
top = (h - icon.height()) / 2;
left = (w - icon.width()) / 2;
}
icon.css({
top: top,
left: left
})
},
init: function (settings) {
settings = settings || {};
this.loadingTpl = settings.loadingTpl || this.loadingTpl;
this.target = settings.target || 'html';
this.bindEvent();
},
bindEvent: function () {
var _this = this;
$(this.target).on('stop', function () {
_this.stop();
});
$(window).on('resize', function () {
_this.loading && _this.setPosition();
});
}
}
return Loading;
});