-
Notifications
You must be signed in to change notification settings - Fork 2
/
loadImages.js
171 lines (156 loc) · 5.41 KB
/
loadImages.js
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
/**
* @version 1.0.0
* @DATE: 2016-02-03
* @author Lidian, [email protected]
*
* @description loadImages can preload images and after each image load completed can callback process
* @param {object} options - past setting parameters
* @param {string[]} options.data - images url array
* @callback options.step - each step callback function
* @param {number} - process number
* @callback options.complete - all images are loaded
* @param {boolean} needOneStep - increase by 1 each time
* @param {string} path - images data string common path
* @return {boolean}
* @example
* loadImages({
* data:["1.png", "2.png", "3.png"],
* step:function(num){},
* complete: function(){},
* needOneStep: true,
* path:"/images"
* });
* //images path will be "/images/1.png" and so on
*/
(function(w){
function loadImages(options){
var len = 0, //资源总数
index = 0, //循环资源数组用
curIndex = 0, //记录当前加载完成资源个数
stepTimer = null, //记录当前setTimeout对象句柄
stepTimeValue = 5, //步进时间间隔
percentageValue = 0, //当前百分比
targetPercent = 0, //目标百分比
data = options.data || [],
step = options.step || function(){},
complete = options.complete || function(){},
needOneStep = options.needOneStep || false,
path = options.path || false;
if(typeof data !== "object" || data.length === 0){
step(100);
return false;
}
len = data.length;
if(path){
for(var i = len-1; i>-1; i--){
data[i] = path + data[i];
console.info(data[i])
}
}
var processStep = function (){
percentageValue++;
// console.info("processStep = ",percentageValue)
step(percentageValue);
if(percentageValue < targetPercent){
stepTimer = setTimeout(function (){
processStep();
}, stepTimeValue);
} else if(targetPercent === 100 && percentageValue === targetPercent){
if(complete && typeof complete === "function"){
complete();
}
}
}
function onload(){
curIndex++;
targetPercent = Math.floor(curIndex/len*100);
if(needOneStep){
if(stepTimer){
clearTimeout(stepTimer);
}
processStep();
} else{
step(targetPercent);
if(targetPercent === 100){
complete();
}
}
}
for(index; index < len; index++){
var strUrl = data[index];
(new loadImageItem(strUrl, onload)).start();
}
}
/**
* @name loadImageItem
* @param {string} url - images full url
* @callback cb - called when load image completed
*/
function loadImageItem(url, cb){
var self = this;
this.img = new Image();
//readyState为complete和loaded则表明图片已经加载完毕。测试IE6-IE10支持该事件,其它浏览器不支持。
var onReadyStateChange = function(){
removeEventHandlers();
console.info("onReadyStateChange");
cb(this, "onReadyStateChange");
};
var onError = function(){
console.info("onError");
removeEventHandlers();
cb(this, "onError");
};
var onLoad = function(){
removeEventHandlers();
cb(this, "onload");
};
var removeEventHandlers = function() {
self.unbind('load', onLoad);
self.unbind('readystatechange', onReadyStateChange);
self.unbind('error', onError);
};
this.start = function(){
this.bind('load', onLoad);
this.bind('readystatechange', onReadyStateChange);
this.bind('error', onError);
this.img.src = url;
if(self.img.complete){
removeEventHandlers();
cb(this, "onload");
}
}
}
/**
* @name bind
* @description cross-browser event binding
* @param {string} eventName
* @param {function} eventHandler
*/
loadImageItem.prototype.bind = function(eventName, eventHandler) {
if (this.img.addEventListener) {
this.img.addEventListener(eventName, eventHandler, false);
} else if (this.img.attachEvent) {
this.img.attachEvent('on' + eventName, eventHandler);
}
};
/**
* @name unbind
* @description cross-browser event un-binding
* @param {string} eventName
* @param {function} eventHandler
*/
loadImageItem.prototype.unbind = function(eventName, eventHandler) {
if (this.img.removeEventListener) {
this.img.removeEventListener(eventName, eventHandler, false);
} else if (this.img.detachEvent) {
this.img.detachEvent('on' + eventName, eventHandler);
}
};
// AMD module support
if (typeof define === 'function' && define.amd) {
define('loadImages', [], function() {
return loadImages;
});
}
w.loadImages = loadImages;
})(window);