-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoneFrame.js
More file actions
85 lines (83 loc) · 2.39 KB
/
Copy pathoneFrame.js
File metadata and controls
85 lines (83 loc) · 2.39 KB
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
let oneFrame = undefined;
let children = undefined;
let lengthChildren = undefined;
let elementActive = 0;
let lastDirection = "up";
function init(idName) {
if (document.querySelector(idName)) {
oneFrame = document.querySelector(idName);
children = oneFrame.children;
lengthChildren = children.length;
events(oneFrame);
}
else console.log(undefined);
}
// events
function events(oneFrame) {
oneFrame.addEventListener("wheel", throttle(handler, 1000));
window.addEventListener("resize", debounce(handler, 100));
}
// handler
function handler({ deltaY, type }) {
if (type === 'wheel') {
let direction = calDirection(deltaY);
if (direction === "down") {
if (!isChild(elementActive + 1)) return;
elementActive = elementActive + 1;
}
else {
if (!isChild(elementActive - 1)) return;
elementActive = elementActive - 1;
}
lastDirection = direction;
}
oneFrame.style.transform = `translate3d(0px, -${calculateY(elementActive, lastDirection)}px, 0px)`;
}
// isChild
function isChild(num) {
return num >= 0 && num <= lengthChildren - 1;
}
// calDirection
function calDirection(deltaY) {
return Math.sign(deltaY) === 1 ? 'down' : 'up'
}
// calculateY
function calculateY(elementActive, direction) {
let top = calculateTop(elementActive);
if (direction === 'down') {
const clientHeight = document.documentElement.clientHeight;
const height = children[elementActive].getBoundingClientRect().height;
return top + height - clientHeight;
} else if (direction === 'up') {
return top;
}
}
// calculateTop
function calculateTop(element) {
let top = 0;
for (let index = 0; index < element; index++) {
top += children[index].getBoundingClientRect().height;
}
return top;
}
// throttle
function throttle(callback, interval) {
let enableCall = true;
return function (...args) {
if (!enableCall) return;
enableCall = false;
callback.apply(this, args);
setTimeout(() => enableCall = true, interval);
}
}
// debounce
function debounce(func, delay) {
let inDebounce
return function () {
const context = this
const args = arguments
clearTimeout(inDebounce)
inDebounce = setTimeout(() => func.apply(context, args), delay)
}
}
export default { init }