forked from urfu-2017/webdev-task-4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
74 lines (64 loc) · 1.83 KB
/
index.js
File metadata and controls
74 lines (64 loc) · 1.83 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
'use strict';
class Menu {
constructor() {
this.input = document.getElementById('menu');
this.body = document.body;
}
init() {
this.__addListeners();
}
__addListeners() {
this.body.addEventListener('touchstart', e => {
this.touch = e.touches[0];
this.startX = this.touch.clientX;
this.startY = this.touch.clientY;
});
this.body.addEventListener('touchend', e => {
this.touchEnd = e.changedTouches[0];
this.endX = this.touchEnd.clientX;
this.endY = this.touchEnd.clientY;
if (this.startX - this.endX > 100 && Math.abs(this.endY - this.startY) < 40) {
this.__show();
}
if (this.startX - this.endX < -100 && Math.abs(this.endY - this.startY) < 40) {
this.__close();
}
});
this.input.addEventListener('change', () => this.__toggleScroll());
}
__clearCoordinates() {
this.startX = 0;
this.startY = 0;
this.endX = 0;
this.endY = 0;
}
__show() {
this.input.checked = true;
this.__clearCoordinates();
this.__toggleScroll();
}
__close() {
this.input.checked = false;
this.__clearCoordinates();
this.__toggleScroll();
}
__toggleScroll() {
if (window.innerWidth >= 768) {
return;
}
if (this.input.checked) {
window.onwheel = this.__preventDefault;
window.ontouchmove = this.__preventDefault;
} else {
window.onwheel = null;
window.ontouchmove = null;
}
}
__preventDefault(e) {
e.preventDefault();
}
}
document.addEventListener('DOMContentLoaded', () => {
const menu = new Menu();
menu.init();
});