-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.isonscreen.js
43 lines (35 loc) · 1.28 KB
/
jquery.isonscreen.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
(function($) {
$.fn.isOnScreen = function(x_offset, y_offset) {
if(x_offset == null || typeof x_offset == 'undefined') x_offset = 1;
if(y_offset == null || typeof y_offset == 'undefined') y_offset = 1;
var win = $(window),
viewport = {
top : win.scrollTop(),
left : win.scrollLeft()
},
height = this.outerHeight(),
width = this.outerWidth(),
bounds = this.offset(),
visible,
deltas;
viewport.right = viewport.left + win.width();
viewport.bottom = viewport.top + win.height();
if(!width || !height) return false;
bounds.right = bounds.left + width;
bounds.bottom = bounds.top + height;
visible = (
!(viewport.right < bounds.left ||
viewport.left > bounds.right ||
viewport.bottom < bounds.top ||
viewport.top > bounds.bottom)
);
if(!visible) return false;
deltas = {
top: Math.min(1, (bounds.bottom - viewport.top) / height),
bottom: Math.min(1, (viewport.bottom - bounds.top) / height),
left: Math.min(1, (bounds.right - viewport.left) / width),
right: Math.min(1, (viewport.right - bounds.left) / width)
};
return (deltas.left * deltas.right) >= x_offset && (deltas.top * deltas.bottom) >= y_offset;
};
})(jQuery);