-
Notifications
You must be signed in to change notification settings - Fork 0
/
skrulle.js
48 lines (44 loc) · 1.47 KB
/
skrulle.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
/**
* Skrulle
*
* By Pelle Bjerkestrand - http://pellebjerkestrand.net/
* License: UNLICENSE - http://unlicense.org/
*/
;(function(window, document){
'use strict';
var skrulle = {
up: 'up',
down: 'down',
previousScrollY: 0,
debounce: function(fn, delay) {
// http://remysharp.com/2010/07/21/throttling-function-calls/
var timer = null;
return function () {
var context = this, args = arguments;
clearTimeout(timer);
timer = setTimeout(function () {
fn.apply(context, args);
}, delay);
};
},
handleScroll: function(oldY, newY){
if(oldY < newY){
// scrolling down
document.documentElement.classList.remove(skrulle.up);
document.documentElement.classList.add(skrulle.down);
} else if(oldY > newY) {
// scrolling up
document.documentElement.classList.remove(skrulle.down);
document.documentElement.classList.add(skrulle.up);
}
// equal values is a no op
skrulle.previousScrollY = newY;
},
init: function(){
window.addEventListener('scroll', skrulle.debounce(function(){
skrulle.handleScroll(skrulle.previousScrollY, window.scrollY);
}, 150));
}
};
skrulle.init();
})(window, document);