forked from ajlkn/jquery.scrollex
-
Notifications
You must be signed in to change notification settings - Fork 3
/
jquery.scrollex.js
324 lines (226 loc) · 6.64 KB
/
jquery.scrollex.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
/* jquery.scrollex v0.2.1 | (c) n33 | n33.co @n33co | MIT */
(function($) {
var $window = $(window),
ids = 1,
queue = {};
/**
* Resolves a top/bottom value, optionally relative to an element's height
* or the window height.
*
* @param {integer} x Value.
* @param {integer} eHeight Element height.
* @param {integer} vHeight Viewport (window) height.
* @return {integer} Resolved value.
*/
function resolve(x, eHeight, vHeight) {
if (typeof x === 'string') {
// Percentage? Relative to element height.
if (x.slice(-1) == '%')
x = (parseInt(x.substring(0, x.length - 1)) / 100.00) * eHeight;
// vh? Relative to viewport height.
else if (x.slice(-2) == 'vh')
x = (parseInt(x.substring(0, x.length - 2)) / 100.00) * vHeight;
// px? Redundant but okay!
else if (x.slice(-2) == 'px')
x = parseInt(x.substring(0, x.length - 2));
}
return x;
};
/**
* Window events.
*/
$window
.on('scroll', function() {
// Get vTop.
var vTop = $window.scrollTop();
// Step through handler queue.
$.map(queue, function(o) {
// Clear existing timeout.
window.clearTimeout(o.timeoutId);
// Call handler after timeout delay.
o.timeoutId = window.setTimeout(function() {
(o.handler)(vTop);
}, o.options.delay);
});
})
.on('load', function() {
$window.trigger('scroll');
});
/**
* Activates scrollex on an element.
*
* @param {object} userOptions Options.
* @return {jQuery} jQuery object.
*/
jQuery.fn.scrollex = function(userOptions) {
var $this = $(this);
// No elements?
if (this.length == 0)
return $this;
// Multiple elements?
if (this.length > 1) {
for (var i=0; i < this.length; i++)
$(this[i]).scrollex(userOptions);
return $this;
}
// Already scrollexed?
if ($this.data('_scrollexId'))
return $this;
// Vars.
var id, options, test, handler, o;
// Build object.
// ID.
id = ids++;
// Options.
options = jQuery.extend({
// Top.
top: 0,
// Bottom.
bottom: 0,
// Delay.
delay: 0,
// Mode ('default', 'top', 'middle', 'bottom', 'top-only', 'bottom-only').
mode: 'default',
// Enter function.
enter: null,
// Leave function.
leave: null,
// Initialize function.
initialize: null,
// Terminate function.
terminate: null,
// Scroll function.
scroll: null
}, userOptions);
// Test.
switch (options.mode) {
// top: Top viewport edge must fall within element's contact area.
case 'top':
test = function(vTop, vMiddle, vBottom, eTop, eBottom) {
return (vTop >= eTop && vTop <= eBottom);
};
break;
// bottom: Bottom viewport edge must fall within element's contact area.
case 'bottom':
test = function(vTop, vMiddle, vBottom, eTop, eBottom) {
return (vBottom >= eTop && vBottom <= eBottom);
};
break;
// middle: Midpoint between top/bottom viewport edges must fall within element's contact area.
case 'middle':
test = function(vTop, vMiddle, vBottom, eTop, eBottom) {
return (vMiddle >= eTop && vMiddle <= eBottom);
};
break;
// top-only: Top viewport edge must be visible
case 'top-only':
test = function(vTop, vMiddle, vBottom, eTop, eBottom) {
return (vTop <= eTop && eTop <= vBottom);
};
break;
// bottom-only: Bottom viewport edge must be visible
case 'bottom-only':
test = function(vTop, vMiddle, vBottom, eTop, eBottom) {
return (vBottom >= eBottom && eBottom >= vTop);
};
break;
// default: Element's contact area must fall within the viewport.
default:
case 'default':
test = function(vTop, vMiddle, vBottom, eTop, eBottom) {
return (vBottom >= eTop && vTop <= eBottom);
};
break;
}
// Handler.
handler = function(vTop) {
var currentState = this.state,
newState = false,
offset = this.$element.offset(),
vHeight, vMiddle, vBottom,
eHeight, eTop, eBottom;
// Viewport values.
vHeight = $window.height();
vMiddle = vTop + (vHeight / 2);
vBottom = vTop + vHeight;
// Element values.
eHeight = this.$element.outerHeight();
eTop = offset.top + resolve(this.options.top, eHeight, vHeight);
eBottom = (offset.top + eHeight) - resolve(this.options.bottom, eHeight, vHeight);
// Determine if there's been a state change.
newState = this.test(vTop, vMiddle, vBottom, eTop, eBottom);
if (newState != currentState) {
// Update state.
this.state = newState;
// Call appropriate function.
if (newState) {
if (this.options.enter)
(this.options.enter).apply(this.element);
}
else {
if (this.options.leave)
(this.options.leave).apply(this.element);
}
}
// Call scroll function.
if (this.options.scroll)
(this.options.scroll).apply(this.element, [
(vMiddle - eTop) / (eBottom - eTop)
]);
};
// Object.
o = {
id: id,
options: options,
test: test,
handler: handler,
state: null,
element: this,
$element: $this,
timeoutId: null
};
// Add object to queue.
queue[id] = o;
// Add scrollex ID to element.
$this.data('_scrollexId', o.id);
// Call initialize.
if (o.options.initialize)
(o.options.initialize).apply(this);
return $this;
};
/**
* Deactivates scrollex on an element.
*
* @return {jQuery} jQuery object.
*/
jQuery.fn.unscrollex = function() {
var $this = $(this);
// No elements?
if (this.length == 0)
return $this;
// Multiple elements?
if (this.length > 1) {
for (var i=0; i < this.length; i++)
$(this[i]).unscrollex();
return $this;
}
// Vars.
var id, o;
// Not scrollexed?
id = $this.data('_scrollexId');
if (!id)
return $this;
// Get object from queue.
o = queue[id];
// Clear timeout.
window.clearTimeout(o.timeoutId);
// Remove object from queue.
delete queue[id];
// Remove scrollex ID from element.
$this.removeData('_scrollexId');
// Call terminate.
if (o.options.terminate)
(o.options.terminate).apply(this);
return $this;
};
})(jQuery);