forked from tuupola/lazyload
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjquery.lazyload.js
264 lines (229 loc) · 9.73 KB
/
jquery.lazyload.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
/*
* Lazy Load - jQuery plugin for lazy loading images
*
* Copyright (c) 2007-2013 Mika Tuupola
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*
* Project home:
* http://www.appelsiini.net/projects/lazyload
*
* Version: 1.8.4
*
*/
(function($, window, document, undefined) {
var $window = $(window);
var defaults = {
threshold : 0,
failure_limit : 0,
event : "scroll",
event_namespace : ".lazyload",
effect : "show",
container : window,
data_attribute : "original",
skip_invisible : true,
appear : null,
load : null,
vertical : true,
horizontal : true,
timeout : 0
};
$.fn.lazyload = function(options) {
var elements = this;
var settings={};
var $container;
var timeout_id;
function update(reset_cache,force) {
var counter = 0;
if(settings.timeout && !force){
if(timeout_id) clearTimeout(timeout_id);
timeout_id = setTimeout(function(){ update(reset_cache,1); }, settings.timeout);
return;
}
timeout_id = null;
elements.each(function() {
var $this = $(this);
if (settings.skip_invisible && !$this.is(":visible")) {
return;
}
if(reset_cache || !this._offset){
this._offset=$this.offset();
this._height=$this.height();
this._width=$this.width();
}
if (settings.vertical && $.abovethetop(this, settings) ||
settings.horizontal && $.leftofbegin(this, settings)) {
/* Nothing. */
} else if ((!settings.vertical || !$.belowthefold(this, settings)) &&
(!settings.horizontal || !$.rightoffold(this, settings))) {
$this.trigger("appear"+settings.event_namespace);
/* if we found an image we'll load, reset the counter */
counter = 0;
} else {
if (++counter > settings.failure_limit) {
return false;
}
}
});
/* Remove image from array so it is not looped next time. Execute with timout to not slow down update function */
setTimeout(function(){
if(elements.cleaning) return;
elements.cleaning=true;
var temp = $.grep(elements, function(element) {
return !element.loaded;
});
elements = $(temp);
});
}
if(options) {
/* Maintain BC for a couple of versions. */
if (undefined !== options.failurelimit) {
options.failure_limit = options.failurelimit;
delete options.failurelimit;
}
if (undefined !== options.effectspeed) {
options.effect_speed = options.effectspeed;
delete options.effectspeed;
}
}
$.extend(settings, defaults, options);
/* Cache container as jQuery as object. */
$container = (settings.container === undefined ||
settings.container === window) ? $window : $(settings.container);
/* Fire one scroll event per scroll. Not one scroll event per image. */
if (0 === settings.event.indexOf("scroll")) {
$container.bind(settings.event+settings.event_namespace, function(event) {
return update();
});
}
this.each(function() {
var self = this;
var $self = $(self);
self.loaded = false;
/* When appear is triggered load original image. */
$self.one("appear"+settings.event_namespace, function() {
if (!this.loaded && $self.data(settings.data_attribute)) {
if (settings.appear) {
var elements_left = elements.length;
settings.appear.call(self, elements_left, settings);
}
$("<img />")
.bind("load"+settings.event_namespace, function() {
$self
.hide()
.attr("src", $self.data(settings.data_attribute))
[settings.effect](settings.effect_speed);
self.loaded = true;
if (settings.load) {
var elements_left = elements.length;
settings.load.call(self, elements_left, settings);
}
})
.attr("src", $self.data(settings.data_attribute));
}
});
/* When wanted event is triggered load original image */
/* by triggering appear. */
if (0 !== settings.event.indexOf("scroll")) {
$self.bind(settings.event+settings.event_namespace, function(event) {
if (!self.loaded) {
$self.trigger("appear"+settings.event_namespace);
}
});
}
});
/* Check if something appears when window is resized. */
$window.bind("resize"+settings.event_namespace, function(event) {
update(true);
});
/* With IOS5 force loading images when navigating with back button. */
/* Non optimal workaround. */
if ((/iphone|ipod|ipad.*os 5/gi).test(navigator.appVersion)) {
$window.bind("pageshow"+settings.event_namespace, function(event) {
if (event.originalEvent.persisted) {
elements.each(function() {
$(this).trigger("appear"+settings.event_namespace);
});
}
});
}
/* Force initial check if images should appear. */
if(!$.isReady){
$(window).load(function() {
update(true);
});
}else{
update(true);
}
return this;
};
// Disable lazyload
$.fn.lazyloadDisable = function(options) {
var namespace, settings={};
$.extend(settings, defaults, options);
// Unbind window events (resize, pageshow...)
$window.unbind(settings.event_namespace);
// Unbind container events (move...)
$(settings.container).unbind(settings.event_namespace);
// Unbind elements events (appear...)
this.unbind(settings.event_namespace);
return this;
}
/* Convenience methods in jQuery namespace. */
/* Use as $.belowthefold(element, {threshold : 100, container : window}) */
$.belowthefold = function(element, settings) {
var fold;
if (settings.container === undefined || settings.container === window) {
fold = $window.height() + $window.scrollTop();
} else {
fold = $(settings.container).offset().top + $(settings.container).height();
}
return fold <= element._offset.top - settings.threshold;
};
$.rightoffold = function(element, settings) {
var fold;
if (settings.container === undefined || settings.container === window) {
fold = $window.width() + $window.scrollLeft();
} else {
fold = $(settings.container).offset().left + $(settings.container).width();
}
return fold <= element._offset.left - settings.threshold;
};
$.abovethetop = function(element, settings) {
var fold;
if (settings.container === undefined || settings.container === window) {
fold = $window.scrollTop();
} else {
fold = $(settings.container).offset().top;
}
return fold >= element._offset.top + settings.threshold + element._height;
};
$.leftofbegin = function(element, settings) {
var fold;
if (settings.container === undefined || settings.container === window) {
fold = $window.scrollLeft();
} else {
fold = $(settings.container).offset().left;
}
return fold >= element._offset.left + settings.threshold + element._width;
};
$.inviewport = function(element, settings) {
return !$.rightoffold(element, settings) && !$.leftofbegin(element, settings) &&
!$.belowthefold(element, settings) && !$.abovethetop(element, settings);
};
/* Custom selectors for your convenience. */
/* Use as $("img:below-the-fold").something() or */
/* $("img").filter(":below-the-fold").something() which is faster */
$.extend($.expr[':'], {
"below-the-fold" : function(a) { return $.belowthefold(a, {threshold : 0}); },
"above-the-top" : function(a) { return !$.belowthefold(a, {threshold : 0}); },
"right-of-screen": function(a) { return $.rightoffold(a, {threshold : 0}); },
"left-of-screen" : function(a) { return !$.rightoffold(a, {threshold : 0}); },
"in-viewport" : function(a) { return $.inviewport(a, {threshold : 0}); },
/* Maintain BC for couple of versions. */
"above-the-fold" : function(a) { return !$.belowthefold(a, {threshold : 0}); },
"right-of-fold" : function(a) { return $.rightoffold(a, {threshold : 0}); },
"left-of-fold" : function(a) { return !$.rightoffold(a, {threshold : 0}); }
});
})(jQuery, window, document);