-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
58 lines (49 loc) · 1.49 KB
/
script.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
class App {
constructor() {
this.sectionItem = document.querySelectorAll('.nav-link');
this.navHead = document.querySelector('.nav-head');
this.hamburger = document.querySelector('.hamburger');
this.close = document.querySelector('.close');
this.sidebar = document.querySelector('.sidebar');
this.init();
}
init() {
this.addScrollToSection();
this.addMenuMobile();
}
addMenuMobile() {
this.addHamburger();
this.removeHamburger();
}
addHamburger() {
this.hamburger.addEventListener('click', event => {
event.currentTarget.classList.add('hamburger--active');
this.sidebar.classList.add('sidebar--active');
this.close.classList.add('close--active');
});
}
removeHamburger() {
this.close.addEventListener('click', event => {
this.hamburger.classList.remove('hamburger--active');
this.sidebar.classList.remove('sidebar--active');
event.currentTarget.classList.remove('close--active');
});
}
addScrollToSection() {
this.sectionItem.forEach(item => {
item.addEventListener('click', event => {
event.preventDefault();
this.scrollToSection(item);
});
});
}
scrollToSection(item) {
const section = item.getAttribute('href');
const sectionElement = document.getElementById(`${section.slice(1)}`);
sectionElement.scrollIntoView({
behavior: 'smooth',
block: 'start',
});
}
}
new App();