forked from Gaya/queryloader2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.queryloader2.js
293 lines (254 loc) · 9.28 KB
/
jquery.queryloader2.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
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
/*
* QueryLoader v2 - A simple script to create a preloader for images
*
* For instructions read the original post:
* http://www.gayadesign.com/diy/queryloader2-preload-your-images-with-ease/
*
* Copyright (c) 2011 - Gaya Kessler
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*
* Version: 2.5
* Last update: 15-09-2013
*
*/
(function($){
$.queryLoader2 = function(el, options){
var base = this;
// Access to jQuery and DOM versions of element
base.$el = $(el);
base.el = el;
// Add a reverse reference to the DOM object
base.$el.data("queryLoader2", base);
//declare variables
base.qLimageContainer = "";
base.qLoverlay = "";
base.qLbar = "";
base.qLpercentage = "";
base.qLimages = [];
base.qLbgimages = [];
base.qLimageCounter = 0;
base.qLdone = 0;
base.qLdestroyed = false;
base.init = function(){
base.options = $.extend({},$.queryLoader2.defaultOptions, options);
//find images
base.findImageInElement(base.el);
if (base.options.deepSearch == true) {
base.$el.find("*:not(script)").each(function() {
base.findImageInElement(this);
});
}
//create containers
base.createPreloadContainer();
base.createOverlayLoader();
};
//the container where unbindable images will go
base.createPreloadContainer = function() {
base.qLimageContainer = $("<div></div>").appendTo("body").css({
display: "none",
width: 0,
height: 0,
overflow: "hidden"
});
//add background images for loading
for (var i = 0; base.qLbgimages.length > i; i++) {
$.ajax({
url: base.qLbgimages[i],
type: 'HEAD',
complete: function (data) {
if (!base.qLdestroyed) {
base.addImageForPreload(this['url']);
}
}
});
}
};
base.addImageForPreload = function(url) {
var image = $("<img>").attr("src", url);
//binding load before the DOM adding
base.bindLoadEvent(image);
image.appendTo(base.qLimageContainer);
};
//create the overlay
base.createOverlayLoader = function () {
var overlayPosition = "absolute";
if (base.$el.prop("tagName") == "BODY") {
overlayPosition = "fixed";
} else {
base.$el.css("position", "relative");
}
base.qLoverlay = $("<div id='" + base.options.overlayId + "'></div>").css({
width: "100%",
height: "100%",
backgroundColor: base.options.backgroundColor,
backgroundPosition: "fixed",
position: overlayPosition,
zIndex: 666999, //very high!
top: 0,
left: 0
}).appendTo(base.$el);
base.qLbar = $("<div id='qLbar'></div>").css({
height: base.options.barHeight + "px",
marginTop: "-" + (base.options.barHeight / 2) + "px",
backgroundColor: base.options.barColor,
width: "0%",
position: "absolute",
top: "50%"
}).appendTo(base.qLoverlay);
if (base.options.percentage == true) {
base.qLpercentage = $("<div id='qLpercentage'></div>").text("0%").css({
height: "40px",
width: "100px",
position: "absolute",
fontSize: "3em",
top: "50%",
left: "50%",
marginTop: "-" + (59 + base.options.barHeight) + "px",
textAlign: "center",
marginLeft: "-50px",
color: base.options.barColor
}).appendTo(base.qLoverlay);
}
if (!base.qLimages.length) {
base.destroyContainers();
}
};
//destroy all containers created by QueryLoader
base.destroyContainers = function () {
base.qLdestroyed = true;
base.qLimageContainer.remove();
base.qLoverlay.remove();
};
base.findImageInElement = function (element) {
var url = "";
var obj = $(element);
var type = "normal";
if (obj.css("background-image") != "none") {
url = obj.css("background-image");
type = "background";
} else if (typeof(obj.attr("src")) != "undefined" && element.nodeName.toLowerCase() == "img") {
url = obj.attr("src");
}
if (url.indexOf("gradient") == -1) {
url = url.replace(/url\(\"/g, "");
url = url.replace(/url\(/g, "");
url = url.replace(/\"\)/g, "");
url = url.replace(/\)/g, "");
var urls = url.split(", ");
for (var i = 0; i < urls.length; i++) {
if (urls[i].length > 0 && base.qLimages.indexOf(urls[i]) == -1 && !urls[i].match(/^(data:)/i)) {
var extra = "";
if (base.isIE() || base.isOpera()){
//filthy always no cache for IE, sorry peeps!
extra = "?rand=" + Math.random();
base.qLbgimages.push(urls[i] + extra);
} else {
if (type == "background") {
base.qLbgimages.push(urls[i]);
} else {
base.bindLoadEvent(obj);
}
}
base.qLimages.push(urls[i]);
}
}
}
}
base.isIE = function () {
return navigator.userAgent.match(/msie/i);
};
base.isOpera = function () {
return navigator.userAgent.match(/Opera/i);
};
base.bindLoadEvent = function (element) {
base.qLimageCounter++;
element.bind("load error", function () {
base.completeImageLoading(this);
});
}
base.completeImageLoading = function (el) {
base.qLdone++;
var percentage = (base.qLdone / base.qLimageCounter) * 100;
base.qLbar.stop().animate({
width: percentage + "%",
minWidth: percentage + "%"
}, 200);
if (base.options.percentage == true) {
base.qLpercentage.text(Math.ceil(percentage) + "%");
}
if (base.qLdone == base.qLimageCounter) {
base.endLoader();
}
};
base.endLoader = function () {
base.qLdestroyed = true;
base.onLoadComplete();
};
base.onLoadComplete = function() {
if (base.options.completeAnimation == "grow") {
var animationTime = 500;
base.qLbar.stop().animate({
"width": "100%"
}, animationTime, function () {
$(this).animate({
top: "0%",
width: "100%",
height: "100%"
}, 500, function () {
$('#' + base.options.overlayId).fadeOut(500, function () {
$(this).remove();
base.destroyContainers();
base.options.onComplete();
})
});
});
} else {
$('#' + base.options.overlayId).fadeOut(500, function () {
$('#' + base.options.overlayId).remove();
base.destroyContainers();
base.options.onComplete();
});
}
}
// Run initializer
base.init();
};
//The default options
$.queryLoader2.defaultOptions = {
onComplete: function() {},
backgroundColor: "#000",
barColor: "#fff",
overlayId: 'qLoverlay',
barHeight: 1,
percentage: false,
deepSearch: true,
completeAnimation: "fade",
minimumTime: 500
};
//function binder
$.fn.queryLoader2 = function(options){
return this.each(function(){
(new $.queryLoader2(this, options));
});
};
})(jQuery);
//HERE COMES THE IE SHITSTORM
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function (elt /*, from*/) {
var len = this.length >>> 0;
var from = Number(arguments[1]) || 0;
from = (from < 0)
? Math.ceil(from)
: Math.floor(from);
if (from < 0)
from += len;
for (; from < len; from++) {
if (from in this &&
this[from] === elt)
return from;
}
return -1;
};
}