-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.preloadit.js
More file actions
157 lines (136 loc) · 4.2 KB
/
jquery.preloadit.js
File metadata and controls
157 lines (136 loc) · 4.2 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
/*
* File: jquery.preloadit-0.0.3.js
*
* Class: preloadit
* This plugin preloads all the images all the images contained in the extended images.
*
* Use:
* > $(element).preloadit() //preloads all the images inside of element
*
* Group: Callbacks
*
* Function: onComplete
* This callback fires when the entire set of images has been preloaded.
*
* Function: onUpdate
* This plugin fire when an image is loaded
*
* Function: onImgLoad
* Method called when an image is loaded
*
* Parameters:
* imagesLoaded - the number of images loaded so far
* totalImages - the number of images contained in the element
*/
(function($) {
var methods = {
init : function(options) {
var defaults = {
onComplete: false,
onUpdate: false,
onImgLoad: false
}, t = this;
$.extend(this, defaults, options);
this.$images = this.find("img");
if(this.is("img")){
this.$images.push(this);
}
this.cache = [];
this.completedRequests = [];
this.loadedImages = [];
//request each image through ajax
this.$images.each(function(i, o){
var $img = $(o),
src = $img.attr('src');
// after request completion check if it was a sucess or otherwise.
$.ajax({
url: src,
complete: function(xhr, status){
switch(xhr.status){
case 200:
onSuccess.call(t, $img);
break;
default:
addXHR.call(t, xhr)
}
}
});
});
}
};
/*
* Group: private methods
*
*/
/*
* onSuccess()
* loads the image into the dom then fires an event
*/
function onSuccess($img){
var t = this;
$img.load(function(){
//remove hidden image from dom
$img.remove();
//unhide img
$img.show()
t.loadedImages.push($img);
if(t.onImgLoad){
t.onImgLoad($img);
}
addXHR.call(t);
});
$("body").append($img.hide());
}
/*
* addXHR()
*/
function addXHR(xhr){
this.completedRequests.push(xhr);
checkComplete.call(this)
}
/*
* checkComplete()
* see if the requests are done
* send updates
*/
function checkComplete(){
//update the number of completed requests
if(this.onUpdate){
this.onUpdate(this.completedRequests.length, this.$images.length);
}
//run the oncomplete images function well all images loaded
if (this.completedRequests.length === this.$images.length) {
if(this.onComplete){
this.onComplete();
}
}
}
/*
* Preload arr assums the jquery object is a div
*/
$.fn.preloadit = function(method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.');
}
};
/*
* Preload arr assums the jquery object is an array of urls
*/
$.fn.preloadArr = function(options){
images = this;
//preload conainer
var $preloadContainer = $("<div>");
//load images
for( i = 0; i < images.length; i++ ){
var $img = $("<img/>");
$img.attr("src", images[i]);
$preloadContainer.append($img);
}
//after preload insert into dom
$preloadContainer.preloadit(options);
};
}(jQuery));