-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
37 lines (30 loc) · 1.02 KB
/
index.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
module.exports = function(callback) {
if (!document.addEventListener) throw new Error('addEventListener is not supported')
var lastScrollY = 0,
scrollHandler,
rAF,
doTick,
isTicking = false,
update,
fallbackTimeoutDuration = 1000 / 60;
scrollHandler = function() {
lastScrollY = window.scrollY;
return doTick();
};
doTick = function() {
if (!isTicking) {
rAF(update);
return isTicking = true;
}
};
update = function() {
callback(lastScrollY);
return isTicking = false;
};
window.addEventListener('scroll', scrollHandler, false);
return rAF = (function() {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function(timeout_callback) {
return window.setTimeout(timeout_callback, fallbackTimeoutDuration);
};
})();
};