-
Notifications
You must be signed in to change notification settings - Fork 17
/
jquery.DEPreLoad.js
56 lines (45 loc) · 1.71 KB
/
jquery.DEPreLoad.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
(function($) {
var items = new Array(), current = 0;
/* Callbacks */
var OnStep = function(Percent) { };
var OnComplete = function() { };
// Get all images from css and <img> tag
var getImages = function(element) {
$(element).find('*:not(script)').each(function() {
var url = "";
if ($(this).css('background-image').indexOf('none') == -1 && $(this).css('background-image').indexOf('-gradient') == -1) {
url = $(this).css('background-image');
if (url.indexOf('url') != -1) {
var temp = url.match(/url\((.*?)\)/);
url = temp[1].replace(/\"/g, '');
}
} else if ($(this).get(0).nodeName.toLowerCase() == 'img' && typeof($(this).attr('src')) != 'undefined') {
url = $(this).attr('src');
}
if (url.length > 0) {
items.push(url);
}
});
};
var loadComplete = function() {
current++;
OnStep(Math.round((current / items.length) * 100));
if (current == items.length) {
OnComplete();
}
};
var loadImg = function(url) {
$(new Image()).load(loadComplete).error(loadComplete).attr('src', url);
};
$.fn.DEPreLoad = function(options) {
return this.each(function() {
/* Set Callbacks */
if (typeof(options.OnStep) !== "undefined") OnStep = options.OnStep;
if (typeof(options.OnComplete) !== "undefined") OnComplete = options.OnComplete;
getImages(this);
for (var i = 0; i < items.length; i++) {
loadImg(items[i]);
}
});
};
})(jQuery);