-
Notifications
You must be signed in to change notification settings - Fork 0
/
zscroll.js
47 lines (37 loc) · 1.46 KB
/
zscroll.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
!function () {
var Z = {};
Z.scroll = {
registerScrollEvents: function () {
window.onscroll = this.getOnscrollClosure();
},
// TODO: isolate calculations as much as possible
getOnscrollClosure: function () {
var self = this;
var lastPositionY = 0;
var lastPositionX = 0;
return function () {
var currentPositionY = window.pageYOffset || document.documentElement.scrollTop;
var currentPositionX = window.pageXOffset || document.documentElement.scrollLeft;
if (currentPositionY < lastPositionY) {
self.fireEvent('zscroll', {direction: 'up'});
}
if (currentPositionY > lastPositionY) {
self.fireEvent('zscroll', {direction: 'down'});
}
if (currentPositionX < lastPositionX) {
self.fireEvent('zscroll', {direction: 'left'});
}
if (currentPositionX > lastPositionX) {
self.fireEvent('zscroll', {direction: 'right'});
}
lastPositionY = currentPositionY;
lastPositionX = currentPositionX;
}
},
fireEvent: function (name, data) {
var event = new CustomEvent(name, {'detail': data});
document.dispatchEvent(event);
}
};
Z.scroll.registerScrollEvents();
}();