-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.scrolltracker.js
130 lines (122 loc) · 5.41 KB
/
jquery.scrolltracker.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
(function ( $ ) {
$.scrolltracker = function( el, options ) {
$(window).on('DOMContentLoaded load resize scroll', function(){
//set default options
var settings = $.extend({
// These are the defaults.
direction: "vertical"
//you can override this with "horizontal" or "both"
}, options );
var locateVert = function(rect){
if (
rect.top >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight)
) return 'onscreen';
else if(
rect.top >= (window.innerHeight || document.documentElement.clientHeight) || rect.bottom <= 0
) return 'offscreen';
else if(
rect.top < 0 && rect.bottom > (window.innerHeight || document.documentElement.clientHeight)
) return 'overflowing';
else if(
rect.top < 0 &&
rect.bottom >= 0
) return 'onfromtop';
else if(
rect.bottom > (window.innerHeight || document.documentElement.clientHeight) &&
rect.top < (window.innerHeight || document.documentElement.clientHeight)
) return 'onfrombottom';
}
var locateHoriz = function(rect){
if (
rect.left >= 0 &&
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
) return 'onscreen';
else if(
rect.left >= (window.innerWidth || document.documentElement.clientWidth) || rect.right <= 0
) return 'offscreen';
else if(
rect.left < 0 && rect.right > (window.innerWidth || document.documentElement.clientWidth)
) return 'overflowing';
else if(
rect.left < 0 &&
rect.right >= 0
) return 'onfromleft';
else if(
rect.right > (window.innerWidth || document.documentElement.clientWidth) &&
rect.left < (window.innerWidth || document.documentElement.clientWidth)
) return 'onfromright';
}
var locateVertHoriz = function(rect){
if (
rect.left >= 0 &&
rect.right <= (window.innerWidth || document.documentElement.clientWidth) &&
rect.top >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight)
) return 'onscreen';
// this may be useful if you need to know if the offscreen element is ofscreen on both axes (diagonally), but that seems unlikely...
// else if(
// (rect.left >= (window.innerWidth || document.documentElement.clientWidth) || rect.right <= 0) &&
// (rect.top >= (window.innerHeight || document.documentElement.clientHeight) || rect.bottom <= 0)
// ) return 'offscreen';
else if(
(rect.left < 0 && rect.right > (window.innerWidth || document.documentElement.clientWidth)) &&
(rect.top < 0 && rect.bottom > (window.innerHeight || document.documentElement.clientHeight))
) return 'overflowing';
}
$(el).each(function(){
var $this = $(this);
//pull the element from the jQuery object, get its bounding rectangle
var rect = $this[0].getBoundingClientRect();
if(settings.direction == "vertical" || settings.direction == "both"){
var screenstatus = locateVert(rect);
if (screenstatus == 'onscreen') {
$this.addClass('onscreen_vert');
} else $this.removeClass('onscreen_vert');
if (screenstatus == 'offscreen') {
$this.addClass('offscreen_vert');
} else $this.removeClass('offscreen_vert');
if (screenstatus == 'overflowing') {
$this.addClass('overflowing_vert');
} else $this.removeClass('overflowing_vert');
if (screenstatus == 'onfromtop') {
$this.addClass('onfromtop');
} else $this.removeClass('onfromtop');
if (screenstatus == 'onfrombottom') {
$this.addClass('onfrombottom');
} else $this.removeClass('onfrombottom');
}
if(settings.direction == "horizontal" || settings.direction == "both"){
var screenstatus = locateHoriz(rect);
if (screenstatus == 'onscreen') {
$this.addClass('onscreen_horiz');
} else $this.removeClass('onscreen_horiz');
if (screenstatus == 'offscreen') {
$this.addClass('offscreen_horiz');
} else $this.removeClass('offscreen_horiz');
if (screenstatus == 'overflowing') {
$this.addClass('overflowing_horiz');
} else $this.removeClass('overflowing_horiz');
if (screenstatus == 'onfromleft') {
$this.addClass('onfromleft');
} else $this.removeClass('onfromleft');
if (screenstatus == 'onfromright') {
$this.addClass('onfromright');
} else $this.removeClass('onfromright');
}
// if(settings.direction == "both"){
// var screenstatus = locateVertHoriz(rect);
// if (screenstatus == 'onscreen') {
// $this.addClass('onscreen_horiz');
// } else $this.removeClass('onscreen_horiz');
// if (screenstatus == 'offscreen_horiz') {
// $this.addClass('offscreen');
// } else $this.removeClass('offscreen_horiz');
// if (screenstatus == 'overflowing_horiz') {
// $this.addClass('overflowing_horiz');
// } else $this.removeClass('overflowing_horiz');
// }
});
});
};
}( jQuery ));