forked from Gaya/queryloader2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.queryloader2.js
236 lines (208 loc) · 6.92 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
/*
* 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.2
* Last update: 03-04-2012
*
*/
(function($) {
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;
};
}
var qLimages = new Array;
var qLdone = 0;
var qLdestroyed = false;
var qLimageContainer = "";
var qLoverlay = "";
var qLbar = "";
var qLpercentage = "";
var qLimageCounter = 0;
var qLstart = 0;
var qLoptions = {
onComplete: function () {},
backgroundColor: "#000",
barColor: "#fff",
barHeight: 1,
percentage: false,
deepSearch: true,
completeAnimation: "fade",
minimumTime: 500,
onLoadComplete: function () {
if (qLoptions.completeAnimation == "grow") {
var animationTime = 500;
var currentTime = new Date();
if ((currentTime.getTime() - qLstart) < qLoptions.minimumTime) {
animationTime = (qLoptions.minimumTime - (currentTime.getTime() - qLstart));
}
$(qLbar).stop().animate({
"width": "100%"
}, animationTime, function () {
$(this).animate({
top: "0%",
width: "100%",
height: "100%"
}, 500, function () {
$(qLoverlay).fadeOut(500, function () {
$(this).remove();
qLoptions.onComplete();
})
});
});
} else {
$(qLoverlay).fadeOut(500, function () {
$(qLoverlay).remove();
qLoptions.onComplete();
});
}
}
};
var afterEach = function () {
//start timer
var currentTime = new Date();
qLstart = currentTime.getTime();
createPreloadContainer();
createOverlayLoader();
};
var createPreloadContainer = function() {
qLimageContainer = $("<div></div>").appendTo("body").css({
display: "none",
width: 0,
height: 0,
overflow: "hidden"
});
for (var i = 0; qLimages.length > i; i++) {
$.ajax({
url: qLimages[i],
type: 'HEAD',
success: function(data) {
if(!qLdestroyed){
qLimageCounter++;
addImageForPreload(this['url']);
}
}
});
}
};
var addImageForPreload = function(url) {
var image = $("<img />").attr("src", url).bind("load", function () {
completeImageLoading();
}).appendTo(qLimageContainer);
};
var completeImageLoading = function () {
qLdone++;
var percentage = (qLdone / qLimageCounter) * 100;
$(qLbar).stop().animate({
width: percentage + "%",
minWidth: percentage + "%"
}, 200);
if (qLoptions.percentage == true) {
$(qLpercentage).text(Math.ceil(percentage) + "%");
}
if (qLdone == qLimageCounter) {
destroyQueryLoader();
}
};
var destroyQueryLoader = function () {
$(qLimageContainer).remove();
qLoptions.onLoadComplete();
qLdestroyed = true;
};
var createOverlayLoader = function () {
qLoverlay = $("<div id='qLoverlay'></div>").css({
width: "100%",
height: "100%",
backgroundColor: qLoptions.backgroundColor,
backgroundPosition: "fixed",
position: "fixed",
zIndex: 666999,
top: 0,
left: 0
}).appendTo("body");
qLbar = $("<div id='qLbar'></div>").css({
height: qLoptions.barHeight + "px",
marginTop: "-" + (qLoptions.barHeight / 2) + "px",
backgroundColor: qLoptions.barColor,
width: "0%",
position: "absolute",
top: "50%"
}).appendTo(qLoverlay);
if (qLoptions.percentage == true) {
qLpercentage = $("<div id='qLpercentage'></div>").text("0%").css({
height: "40px",
width: "100px",
position: "absolute",
fontSize: "3em",
top: "50%",
left: "50%",
marginTop: "-" + (59 + qLoptions.barHeight) + "px",
textAlign: "center",
marginLeft: "-50px",
color: qLoptions.barColor
}).appendTo(qLoverlay);
}
};
var findImageInElement = function (element) {
var url = "";
if ($(element).css("background-image") != "none") {
var url = $(element).css("background-image");
} else if (typeof($(element).attr("src")) != "undefined" && element.nodeName.toLowerCase() == "img") {
var url = $(element).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 && qLimages.indexOf(urls[i]) == -1) {
var extra = "";
if ($.browser.msie && $.browser.version < 9) {
extra = "?" + Math.floor(Math.random() * 3000);
}
qLimages.push(urls[i] + extra);
}
}
}
}
$.fn.queryLoader2 = function(options) {
if(options) {
$.extend(qLoptions, options );
}
this.each(function() {
findImageInElement(this);
if (qLoptions.deepSearch == true) {
$(this).find("*:not(script)").each(function() {
findImageInElement(this);
});
}
});
afterEach();
return this;
};
})(jQuery);