This repository has been archived by the owner on Feb 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
79 lines (72 loc) · 2.01 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
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
/**
* @property container
* @property pages[]
* @property easing - easing function
* @property duration
* @property offset
*/
export default class PageScroller {
scroll(direction) {
this.allow = false;
let offsets = Array.from(this.pages)
.map(e => e.offsetTop)
.map(e => this.container.scrollTop - e);
let index = offsets.indexOf(
offsets.reduce((prev, curr) =>
Math.abs(curr) < Math.abs(prev) ? curr : prev
)
);
index = direction === "next" ? index + 1 : index > 0 ? index - 1 : 0;
index %= this.pages.length;
let start = new Date().getTime();
let startPoint = this.container.scrollTop;
let endPoint =
this.pages[index].offsetTop - startPoint - (this.offset || 0);
new Promise(resolve => {
const frame = () => {
let time = new Date().getTime() - start;
this.container.scrollTop =
this.easing(time / this.duration) * endPoint + startPoint;
if (time > this.duration) {
resolve();
} else {
requestAnimationFrame(frame);
}
};
requestAnimationFrame(frame);
}).then(() => (this.allow = true));
}
keyboardEvent(event) {
const allow = ["PageUp", "ArrowUp", "Space", "PageDown", "ArrowDown"];
if (allow.includes(event.key)) {
event.preventDefault();
}
switch (event.key) {
case allow[0]:
case allow[1]:
this.allow && this.scroll("prev");
break;
case allow[2]:
case allow[3]:
case allow[4]:
this.allow && this.scroll("next");
break;
}
}
mouseEvent(event) {
if (this.allow) {
event.deltaY >= 0 ? this.scroll("next") : this.scroll("prev");
}
event.returnValue = false;
event.preventDefault();
return false;
}
start() {
document.addEventListener("keydown", this.keyboardEvent.bind(this));
document.addEventListener("wheel", this.mouseEvent.bind(this), false);
}
constructor(options) {
Object.assign(this, options);
this.allow = true;
}
}