-
Notifications
You must be signed in to change notification settings - Fork 4
/
gumby.inview.js
222 lines (170 loc) · 4.95 KB
/
gumby.inview.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
/**
* Gumby InView
*/
!function($) {
'use strict';
function InViewWatcher($el){
Gumby.debug('Initializing InViewWatcher', $el);
//store the element inside this class
this.$el = $el;
var scope = this;
this.setup();
// re-initialize module
this.$el.on('gumby.initialize', function() {
Gumby.debug('Re-initializing InViewWatcher', scope.$el);
scope.setup();
});
}
InViewWatcher.prototype.setup = function(){
Gumby.debug('Setting up instance of InViewWatcher', this.$el);
this.targets = this.parseTargets();
var classname = Gumby.selectAttr.apply(this.$el, ['classname']);
//at this point the classname is either a string, or false;
//if its false, easy.
if(!classname){
this.classname = "active";
this.classnameBottom = "";
this.classnameTop = "";
}else{
//if its a string, it could be seperated by a pipe
if(classname.indexOf("|") > -1){
classname = classname.split("|");
//now classname is an array
this.classname = classname[0];
this.classnameBottom = classname[1] || "";
this.classnameTop = classname[2] || classname[1] || "";
}else{
//if no pipe, then use only classname
this.classname = classname;
this.classnameBottom = "";
this.classnameTop = "";
}
}
//evaluate offsets
var offset = Gumby.selectAttr.apply(this.$el, ['offset']);
if(offset != false){
offset = offset.split("|");
}else{
offset = 0;
}
this.offset = offset[0] || 0;
this.offsetTop = offset[1] || offset[0] || 0;
this.offsetBottom = this.offset;
};
// parse data-for attribute
InViewWatcher.prototype.parseTargets = function() {
var targetStr = Gumby.selectAttr.apply(this.$el, ['target']);
// no targets so return false
if(!targetStr) {
return false;
}
//are there secondary targets?
var secondaryTargets = targetStr.split('|');
// no secondary targets specified so return single target
if(secondaryTargets.length === 1) {
return $(secondaryTargets[0]);
}
// return all targets
return $(secondaryTargets.join(", "));
};
/*
Utility Method for managing Scrolling
*/
//Variables for Initialization
var t, k, tmp, tar, ot, oh, offt, offb, wh = $(window).height(), watchers = [];
//on scroll - loop through elements
// if the element is on the screen, give it the class
// if its off, take it's class away
$(window).on('scroll', function(e){
t = $(this).scrollTop();
for(k = 0; k < watchers.length; k++){
tmp = watchers[k];
tar = tmp.targets || tmp.$el;
//keep 'hidden' elements from breaking the plugin
if(tmp.$el.css('display') == 'none'){
break;
}
//element's offset top and height
ot = tmp.$el.offset().top;
oh = tmp.$el.height();
//how much on the screen before we apply the class
offt = tmp.offsetTop;
offb = tmp.offsetBottom;
//if above bottom and below top, you're on the screen
var below = ot > (t - offb) + wh;
var above = ot + oh - offt < t;
if(!above && !below){
if(tar.hasClass(tmp.classnameTop)){
tar.removeClass(tmp.classnameTop);
}
if(tar.hasClass(tmp.classnameBottom)){
tar.removeClass(tmp.classnameBottom);
}
if(!tar.hasClass(tmp.classname)){
tar.addClass(tmp.classname);
tar.trigger("gumby.inview");
}
}else if(above && !below){
if(tar.hasClass(tmp.classname)){
tar.removeClass(tmp.classname);
}
if(tar.hasClass(tmp.classnameBottom)){
tar.removeClass(tmp.classnameBottom);
}
if(!tar.hasClass(tmp.classnameTop)){
tar.addClass(tmp.classnameTop);
tar.trigger("gumby.offtop");
}
}else if(below && !above){
if(tar.hasClass(tmp.classname)){
tar.removeClass(tmp.classname);
}
if(tar.hasClass(tmp.classnameTop)){
tar.removeClass(tmp.classnameTop);
}
if(!tar.hasClass(tmp.classnameBottom)){
tar.addClass(tmp.classnameBottom);
tar.trigger("gumby.offbottom");
}
}
}
});
//on resize - update window height reference
//and trigger scroll
$(window).on('resize', function(){
wh = $(this).height();
$(window).trigger('scroll');
});
//on page refresh - trigger scroll
$(window).load(function () {
$(window).trigger('scroll');
});
// add toggle Initialization
Gumby.addInitalisation('inview', function(all) {
$('.inview').each(function() {
var $this = $(this);
// this element has already been initialized
// and we're only initialization new modules
if($this.data('isInView') && !all) {
return true;
// this element has already been initialized
// and we need to reinitialize it
} else if($this.data('isInView') && all) {
$this.trigger('gumby.initialize');
}
// mark element as initialized
$this.data('isInView', true);
watchers.push(new InViewWatcher($this));
});
$(window).trigger('scroll');
});
// register UI module
Gumby.UIModule({
module: 'inview',
events: ['initialize', 'trigger', 'onTrigger'],
init: function() {
// Run initialize methods
Gumby.initialize('inview');
}
});
}(jQuery);