-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
114 lines (88 loc) · 2.47 KB
/
main.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const sections = $(".section");
const display = $(".maincontent");
let inScroll = false;
const md = new MobileDetect(window.navigator.userAgent);
const isMobile = md.mobile();
const countSectionPosition = (sectionEq) => {
const position = sectionEq * -100;
if (isNaN(position))
console.error("передано неверное значение в countSectionPositon");
return position;
};
const resetActiveClass = (item, eq) => {
item.eq(eq).addClass("active").siblings().removeClass("active");
};
const performTransition = (sectionEq) => {
if (inScroll) return;
inScroll = true;
const position = countSectionPosition(sectionEq);
const transitionOver = 1000;
const mouseInertionOver = 300;
resetActiveClass(sections, sectionEq);
display.css({
transform: `translateY(${position}%)`,
});
setTimeout(() => {
resetActiveClass($(".fixed-menu__item"), sectionEq);
inScroll = false;
}, transitionOver + mouseInertionOver);
};
const scroller = () => {
const activeSection = sections.filter(".active");
const nextSection = activeSection.next();
const prevSection = activeSection.prev();
return {
next() {
if (nextSection.length) {
performTransition(nextSection.index());
}
},
prev() {
if (prevSection.length) {
performTransition(prevSection.index());
}
},
};
};
$(window).on("wheel", (e) => {
const deltaY = e.originalEvent.deltaY;
const windowScroller = scroller();
if (deltaY > 0) {
windowScroller.next();
}
if (deltaY < 0) {
windowScroller.prev();
}
});
$(document).on("keydown", (e) => {
const tagName = e.target.tagName.toLowerCase();
const windowScroller = scroller();
const userTypingInInputs = tagName === "input" || tagName === "textarea";
if (userTypingInInputs) return;
switch (e.keyCode) {
case 38:
windowScroller.prev();
break;
case 40:
windowScroller.next();
break;
}
});
$("[data-scroll-to]").on("click", (e) => {
e.preventDefault();
const $this = $(e.currentTarget);
const target = $this.attr("data-scroll-to");
performTransition(target);
});
if (isMobile) {
// https://github.com/mattbryson/TouchSwipe-Jquery-Plugin
$("body").swipe({
swipe: (event, direction) => {
let scrollDirection;
const windowScroller = scroller();
if (direction === "up") scrollDirection = "next";
if (direction === "down") scrollDirection = "prev";
windowScroller[scrollDirection]();
},
});
}