forked from xobotyi/viewport.jquery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.viewport.js
325 lines (286 loc) · 11.3 KB
/
jquery.viewport.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
* viewport - jQuery plugin for elements positioning in viewport
* ver.: 0.2
* (c) Copyright 2014, Anton Zinoviev aka xobotyi
* Released under the MIT license
*/
(function( $ ) {
var methods = {
getElementPosition: function( forceViewport ) {
var $this = $( this );
var _scrollableParent = forceViewport ? $this.parents( forceViewport ) : $this.parents( ':have-scroll' );
if( !_scrollableParent.length ) {
return false;
}
var pos = methods['getRelativePosition'].call( this, forceViewport );
var _topBorder = pos.top - _scrollableParent.scrollTop();
var _leftBorder = pos.left - _scrollableParent.scrollLeft();
return {
"elemTopBorder": _topBorder,
"elemBottomBorder": _topBorder + $this.height(),
"elemLeftBorder": _leftBorder,
"elemRightBorder": _leftBorder + $this.width(),
"viewport": _scrollableParent,
"viewportHeight": _scrollableParent.height(),
"viewportWidth": _scrollableParent.width()
};
},
getRelativePosition: function( forceViewport ) {
var fromTop = 0;
var fromLeft = 0;
var $obj = null;
for( var obj = $( this ).get( 0 ); obj && !$( obj ).is( forceViewport ? forceViewport : ':have-scroll' ); obj = $( obj ).parent().get( 0 ) ) {
$obj = $( obj );
if( typeof $obj.data( 'pos' ) == 'undefined' || new Date().getTime() - $obj.data( 'pos' )[1] > 1000 ) {
/*
* Making some kind of a cache system, it takes a bit of memory but helps us veeery much, reducing calculation
* */
fromTop += obj.offsetTop;
fromLeft += obj.offsetLeft;
$obj.data( 'pos', [
[ obj.offsetTop, obj.offsetLeft ],
new Date().getTime()
] );
} else {
fromTop += $obj.data( 'pos' )[0][0];
fromLeft += $obj.data( 'pos' )[0][1];
}
}
return { "top": Math.round( fromTop ), "left": Math.round( fromLeft ) };
},
aboveTheViewport: function( threshold ) {
var pos = methods['getElementPosition'].call( this );
return pos ? pos.elemTopBorder - threshold < 0 : false;
},
partlyAboveTheViewport: function( threshold ) {
var pos = methods['getElementPosition'].call( this );
return pos ? pos.elemTopBorder - threshold < 0
&& pos.elemBottomBorder - threshold >= 0 : false;
},
belowTheViewport: function( threshold ) {
var pos = methods['getElementPosition'].call( this );
return pos ? pos.viewportHeight < pos.elemBottomBorder + threshold : false;
},
partlyBelowTheViewport: function( threshold ) {
var pos = methods['getElementPosition'].call( this );
return pos ? pos.viewportHeight < pos.elemBottomBorder + threshold
&& pos.viewportHeight > pos.elemTopBorder + threshold : false;
},
leftOfViewport: function( threshold ) {
var pos = methods['getElementPosition'].call( this );
return pos ? pos.elemLeftBorder - threshold <= 0 : false;
},
partlyLeftOfViewport: function( threshold ) {
var pos = methods['getElementPosition'].call( this );
return pos ? pos.elemLeftBorder - threshold < 0
&& pos.elemRightBorder - threshold >= 0 : false;
},
rightOfViewport: function( threshold ) {
var pos = methods['getElementPosition'].call( this );
return pos ? pos.viewportWidth < pos.elemRightBorder + threshold : false;
},
partlyRightOfViewport: function( threshold ) {
var pos = methods['getElementPosition'].call( this );
return pos ? pos.viewportWidth < pos.elemRightBorder + threshold
&& pos.viewportWidth > pos.elemLeftBorder + threshold : false;
},
inViewport: function( threshold ) {
var pos = methods['getElementPosition'].call( this );
return pos ? !( pos.elemTopBorder - threshold < 0 )
&& !( pos.viewportHeight < pos.elemBottomBorder + threshold )
&& !( pos.elemLeftBorder - threshold < 0 )
&& !( pos.viewportWidth < pos.elemRightBorder + threshold ) : true;
},
getState: function( threshold, forceViewport, allowPartly ) {
var ret = { "inside": false, "posY": '', "posX": '' };
var pos = methods['getElementPosition'].call( this, forceViewport );
if( !pos ) {
ret.inside = true;
return ret;
}
var _above = pos.elemTopBorder - threshold < 0;
var _below = pos.viewportHeight < pos.elemBottomBorder + threshold;
var _left = pos.elemLeftBorder - threshold < 0;
var _right = pos.viewportWidth < pos.elemRightBorder + threshold;
if( allowPartly ) {
var _partlyAbove = pos.elemTopBorder - threshold < 0 && pos.elemBottomBorder - threshold >= 0;
var _partlyBelow = pos.viewportHeight < pos.elemBottomBorder + threshold && pos.viewportHeight > pos.elemTopBorder + threshold;
var _partlyLeft = pos.elemLeftBorder - threshold < 0 && pos.elemRightBorder - threshold >= 0;
var _partlyRight = pos.viewportWidth < pos.elemRightBorder + threshold && pos.viewportWidth > pos.elemLeftBorder + threshold;
}
if( !_above && !_below && !_left && !_right ) {
ret.inside = true;
return ret;
}
if( allowPartly ) {
if( _partlyAbove && _partlyBelow ) {
ret.posY = 'exceeds';
} else if( ( _partlyAbove && !_partlyBelow ) || ( _partlyBelow && !_partlyAbove ) ) {
ret.posY = _partlyAbove ? 'partly-above' : 'partly-below';
} else if( !_above && !_below ) {
ret.posY = 'inside';
} else {
ret.posY = _above ? 'above' : 'below';
}
if( _partlyLeft && _partlyRight ) {
ret.posX = 'exceeds';
} else if( ( _partlyLeft && !_partlyRight ) || ( _partlyLeft && !_partlyRight ) ) {
ret.posX = _partlyLeft ? 'partly-above' : 'partly-below';
} else if( !_left && !_right ) {
ret.posX = 'inside';
} else {
ret.posX = _left ? 'left' : 'right';
}
} else {
if( _above && _below ) {
ret.posY = 'exceeds';
} else if( !_above && !_below ) {
ret.posY = 'inside';
} else {
ret.posY = _above ? 'above' : 'below';
}
if( _left && _right ) {
ret.posX = 'exceeds';
} else if( !_left && !_right ) {
ret.posX = 'inside';
} else {
ret.posX = _left ? 'left' : 'right';
}
}
return ret;
},
haveScroll: function() {
return this.scrollHeight > this.offsetHeight
|| this.scrollWidth > this.offsetWidth;
},
generateEUID: function() {
var result = "";
for( var i = 0; i < 32; i++ ) {
result += Math.floor( Math.random() * 16 ).toString( 16 );
}
return result;
}
};
$.extend( $.expr[':'], {
"in-viewport": function( obj, index, meta ) {
var _threshold = typeof meta[3] == 'string' ? parseInt( meta[3], 10 ) : 0;
return methods['inViewport'].call( obj, _threshold );
},
"above-the-viewport": function( obj, index, meta ) {
var _threshold = typeof meta[3] == 'string' ? parseInt( meta[3], 10 ) : 0;
return methods['aboveTheViewport'].call( obj, _threshold );
},
"below-the-viewport": function( obj, index, meta ) {
var _threshold = typeof meta[3] == 'string' ? parseInt( meta[3], 10 ) : 0;
return methods['belowTheViewport'].call( obj, _threshold );
},
"left-of-viewport": function( obj, index, meta ) {
var _threshold = typeof meta[3] == 'string' ? parseInt( meta[3], 10 ) : 0;
return methods['leftOfViewport'].call( obj, _threshold );
},
"right-of-viewport": function( obj, index, meta ) {
var _threshold = typeof meta[3] == 'string' ? parseInt( meta[3], 10 ) : 0;
return methods['rightOfViewport'].call( obj, _threshold );
},
"partly-above-the-viewport": function( obj, index, meta ) {
var _threshold = typeof meta[3] == 'string' ? parseInt( meta[3], 10 ) : 0;
return methods['partlyAboveTheViewport'].call( obj, _threshold );
},
"partly-below-the-viewport": function( obj, index, meta ) {
var _threshold = typeof meta[3] == 'string' ? parseInt( meta[3], 10 ) : 0;
return methods['partlyBelowTheViewport'].call( obj, _threshold );
},
"partly-left-of-viewport": function( obj, index, meta ) {
var _threshold = typeof meta[3] == 'string' ? parseInt( meta[3], 10 ) : 0;
return methods['partlyLeftOfViewport'].call( obj, _threshold );
},
"partly-right-of-viewport": function( obj, index, meta ) {
var _threshold = typeof meta[3] == 'string' ? parseInt( meta[3], 10 ) : 0;
return methods['partlyRightOfViewport'].call( obj, _threshold );
},
"have-scroll": function( obj ) {
return methods['haveScroll'].call( obj );
}
} );
$.fn.viewportTrack = function( options ) {
var settings = {
"threshold": 0,
"allowPartly": false,
"forceViewport": false,
"tracker": false,
"checkOnInit": true
};
if( typeof options == 'undefined' ) {
return methods['getState'].apply( this, [ settings.threshold, settings.forceViewport, settings.allowPartly ] );
} else if( typeof options == 'string' ) {
if( options == 'destroy' ) {
return this.each( function() {
var $this = $( this );
if( typeof $this.data( 'viewport_euid' ) == 'undefined' ) {
return true;
}
var _scrollable = $( [] );
if( typeof $this.data( 'viewport' ) != 'undefined' ) {
$this.data( 'viewport' ).forEach( function( val ) {
_scrollable = $.extend( _scrollable, $this.parents( val ) );
} );
} else {
_scrollable = $.extend( _scrollable, $this.parents( ":have-scroll" ) );
}
_scrollable.each( function() {
if( $( this ).get( 0 ).tagName == "BODY" ) {
$( window ).unbind( ".viewport" + $this.data( 'viewport_euid' ) );
} else {
$( this ).unbind( ".viewport" + $this.data( 'viewport_euid' ) );
}
} );
$this.removeData( 'viewport_euid' );
} );
} else {
$.error( 'Incorrect parameter value.' );
return this;
}
} else if( typeof options == 'object' ) {
$.extend( settings, options );
if( !settings.tracker && typeof settings.tracker != 'function' ) {
return methods['getState'].apply( this, [ settings.threshold, settings.forceViewport, settings.allowPartly ] );
} else {
return this.each( function() {
var $this = $( this );
var obj = this;
if( typeof $this.data( 'viewport_euid' ) == 'undefined' ) {
$this.data( 'viewport_euid', methods['generateEUID'].call() );
}
if( settings.forceViewport ) {
if( typeof $this.data( 'viewport' ) == 'undefined' ) {
$this.data( 'viewport', [ settings.forceViewport ] );
} else {
$this.data( 'viewport' ).push( settings.forceViewport );
}
}
if( settings.checkOnInit ) {
settings.tracker.apply( obj, [ methods['getState'].apply( obj, [ settings.threshold, settings.forceViewport, settings.allowPartly ] ) ] );
}
var _scrollable = settings.forceViewport ? $this.parents( settings.forceViewport ) : $this.parents( ':have-scroll' );
if( !_scrollable.length ) {
if( settings.forceViewport ) {
$.error( 'No such parent \'' + settings.forceViewport + '\'' );
} else {
settings.tracker.apply( obj, [ { "inside": true, "posY": '', "posX": '' } ] );
return true;
}
}
if( _scrollable.get( 0 ).tagName == "BODY" ) {
$( window ).bind( "scroll.viewport" + $this.data( 'viewport_euid' ), function() {
settings.tracker.apply( obj, [ methods['getState'].apply( obj, [ settings.threshold, settings.forceViewport, settings.allowPartly ] ) ] );
} );
} else {
_scrollable.bind( "scroll.viewport" + $this.data( 'viewport_euid' ), function() {
settings.tracker.apply( obj, [ methods['getState'].apply( obj, [ settings.threshold, settings.forceViewport, settings.allowPartly ] ) ] );
} );
}
} );
}
}
};
})( jQuery );