Repository navigation
Expand file tree
/
Copy pathjquery.preload.js
More file actions
176 lines (119 loc) · 2.67 KB
/
Copy pathjquery.preload.js
File metadata and controls
176 lines (119 loc) · 2.67 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
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
/**
* jQuery.Preload
* http://github.com/htmlhero/jQuery.preload
*
* Created by Andrew Motoshin
* http://htmlhero.ru
*
* Version: 1.4.0
* Requires: jQuery 1.6+
*
*/
(function($){
$.preload = (function(sources, part, callback){
// Plugin cache
var cache = [];
// Wrapper for cache
var caching = function(image){
for (var i = 0; i < cache.length; i++) {
if (cache[i].src === image.src) {
return cache[i];
}
}
cache.push(image);
return image;
};
// Execute callback
var exec = function(sources, callback, last){
if (typeof callback === 'function') {
callback.call(sources, last);
}
};
// Closure to hide cache
return function(sources, part, callback){
// Check input data
if (typeof sources === 'undefined') {
return;
}
if (typeof sources === 'string') {
sources = [sources];
}
if (arguments.length === 2 && typeof part === 'function') {
callback = part;
part = 0;
}
// Split to pieces
var total = sources.length,
next;
if (part > 0 && part < total) {
next = sources.slice(part, total);
sources = sources.slice(0, part);
total = sources.length;
}
// If sources array is empty
if (!total) {
exec(sources, callback, true);
return;
}
// Image loading callback
var preload = arguments.callee,
count = 0;
var loaded = function(){
count++;
if (count !== total) {
return;
}
exec(sources, callback, !next);
preload(next, part, callback);
};
// Loop sources to preload
var image;
for (var i = 0; i < sources.length; i++) {
image = new Image();
image.src = sources[i];
image = caching(image);
if (image.complete) {
loaded();
} else {
$(image).on('load error', loaded);
}
}
};
})();
// Get URLs from DOM elements
var getSources = function(items){
var sources = [],
reg = new RegExp('url\\([\'"]?([^"\'\)]*)[\'"]?\\)', 'i'),
bgs, bg, url, i;
items = items.find('*').add(items);
items.each(function(){
bgs = $(this).css('backgroundImage');
bgs = bgs.split(', ');
for (i = 0; i < bgs.length; i++) {
bg = bgs[i];
if (bg.indexOf('about:blank') !== -1 ||
bg.indexOf('data:image') !== -1) {
continue;
}
url = reg.exec(bg);
if (url) {
sources.push(url[1]);
}
}
if (this.nodeName === 'IMG') {
sources.push(this.src);
}
});
return sources;
};
$.fn.preload = function(callback){
var items = this,
sources = getSources(items);
$.preload(sources, function(){
if (typeof callback === 'function') {
callback.call(items.get());
}
});
return this;
};
})(jQuery);