-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
jquery-lazyloadanything.js
211 lines (157 loc) · 5.53 KB
/
jquery-lazyloadanything.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
/*!
jQuery LazyLoadAnything - 2013-03-04
(c) 2013 Shawn Welch http://shrimpwagon.wordpress.com/jquery-lazyloadanything
license: http://www.opensource.org/licenses/mit-license.php
*/
(function( $ ) {
// Element to listen to scroll event
var $listenTo;
// Force load flag
var force_load_flag = false;
// Plugin methods
var methods = {
'init': function(options) {
var defaults = {
'auto': true,
'cache': false,
'timeout': 1000,
'includeMargin': false,
'viewportMargin': 0,
'repeatLoad': false,
'listenTo': window,
'onLoadingStart': function(e, llelements, indexes) {
return true;
},
'onLoad': function(e, llelement) {
return true;
},
'onLoadingComplete': function(e, llelements, indexes) {
return true;
}
}
var settings = $.extend({}, defaults, options);
$listenTo = $(settings.listenTo);
var timeout = 0;
var llelements = [];
var $selector = this;
// Scroll listener
$listenTo.bind('scroll.lla', function(e) {
// Check for manually/auto load
if(!force_load_flag && !settings.auto) return false;
force_load_flag = false;
// Clear timeout if scrolling continues
clearTimeout(timeout);
// Set the timeout for onLoad
timeout = setTimeout(function() {
/*
x1, y1 o----------------------o x2, y1
| |
| |
| |
| |
| |
| |
| |
| |
x1, y2 o----------------------o x2, y2
*/
var viewport_left = $listenTo.scrollLeft();
var viewport_top = $listenTo.scrollTop();
var viewport_width = $listenTo.innerWidth();
var viewport_height = $listenTo.innerHeight();
var viewport_x1 = viewport_left - settings.viewportMargin;
var viewport_x2 = viewport_left + viewport_width + settings.viewportMargin;
var viewport_y1 = viewport_top - settings.viewportMargin;
var viewport_y2 = viewport_top + viewport_height + settings.viewportMargin;
var load_elements = [];
var i, llelem_top, llelem_bottom;
// Cycle through llelements and check if they are within viewpane
for(i = 0; i < llelements.length; i++) {
// Get top and bottom of llelem
var llelem_x1 = llelements[i].getLeft();
var llelem_x2 = llelements[i].getRight();
var llelem_y1 = llelements[i].getTop();
var llelem_y2 = llelements[i].getBottom();
if(llelements[i].$element.is(':visible'))
{
if((viewport_x1 < llelem_x2) && (viewport_x2 > llelem_x1) && (viewport_y1 < llelem_y2) && (viewport_y2 > llelem_y1)) {
// Grab index of llelements that will be loaded
if(settings.repeatLoad || !llelements[i].loaded) load_elements.push(i);
}
}
}
// Call onLoadingStart event
if(settings.onLoadingStart.call(undefined, e, llelements, load_elements)) {
// Cycle through array of indexes that will be loaded
for(i = 0; i < load_elements.length; i++) {
// Set loaded flag
llelements[load_elements[i]].loaded = true;
// Call the individual element onLoad
if(settings.onLoad.call(undefined, e, llelements[load_elements[i]]) === false) break;
}
// Call onLoadingComplete event
settings.onLoadingComplete.call(undefined, e, llelements, load_elements);
}
}, settings.timeout);
});
// LazyLoadElement class
function LazyLoadElement($element) {
var self = this;
this.loaded = false;
this.$element = $element;
this.top = undefined;
this.bottom = undefined;
this.left = undefined;
this.right = undefined;
this.getTop = function() {
if(self.top) return self.top;
return self.$element.position().top;
}
this.getBottom = function() {
if(self.bottom) return self.bottom;
var top = (self.top) ? self.top : this.getTop();
return top + self.$element.outerHeight(settings.includeMargin);
}
this.getLeft = function() {
if(self.left) return self.left;
return self.$element.position().left;
}
this.getRight = function() {
if(self.right) return self.right;
var left = (self.left) ? self.left : this.getLeft();
return left + self.$element.outerWidth(settings.includeMargin);
}
// Cache the top and bottom of set
if(settings.cache) {
this.top = this.getTop();
this.bottom = this.getBottom();
this.left = this.getLeft();
this.right = this.getRight();
}
}
// Cycle throught the selector(s)
var chain = $selector.each(function() {
// Add LazyLoadElement classes to the main array
llelements.push(new LazyLoadElement($(this)));
});
return chain;
},
'destroy': function() {
$listenTo.unbind('scroll.lla');
},
'load': function() {
force_load_flag = true;
$listenTo.trigger('scroll.lla');
}
}
$.fn.lazyloadanything = function(method) {
// Method calling logic
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.lazyloadanything');
}
};
})( jQuery );