-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
46 lines (39 loc) · 1.69 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
/*=============== SCROLL SECTIONS ACTIVE LINK ===============*/
const sections = document.querySelectorAll('section[id]')
function scrollActive() {
const scrollY = window.pageYOffset;
sections.forEach(current => {
const sectionHeight = current.offsetHeight,
sectionTop = current.offsetTop - 50,
sectionId = current.getAttribute('id');
if (scrollY > sectionTop && scrollY <= sectionTop + sectionHeight) {
document.querySelector('.nav__menu a[href*=' + sectionId + ']').classList.add('active-link');
} else {
document.querySelector('.nav__menu a[href*=' + sectionId + ']').classList.remove('active-link');
}
});
}
window.addEventListener('scroll', scrollActive);
/*=============== CHANGE BACKGROUND HEADER ===============*/
function scrollHeader() {
const header = document.getElementById('header');
// When the scroll is greater than 80 viewport height, add the scroll-header class to the header tag
if (this.scrollY >= 80) header.classList.add('scroll-header');
else header.classList.remove('scroll-header');
}
window.addEventListener('scroll', scrollHeader);
/*=============== HAMBURGER TOGGLE ===============*/
const hamburger = document.querySelector('.hamburger');
const navMenu = document.querySelector('.nav__menu');
const navLinks = document.querySelectorAll('.nav__link');
hamburger.addEventListener('click', function() {
navMenu.classList.toggle('active');
});
// Close nav-menu when a nav-link is clicked
navLinks.forEach(link => {
link.addEventListener('click', () => {
if (navMenu.classList.contains('active')) {
navMenu.classList.remove('active');
}
});
});