-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
29 lines (24 loc) · 1.15 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
/*==================== CHANGE BACKGROUND HEADER ====================*/
function scrollHeader(){
const nav = document.getElementById('navbar')
// When the scroll is greater than 200 viewport height, add the scroll-header class to the header tag
if(this.scrollY >= 80) nav.classList.add('scroll-header'); else nav.classList.remove('scroll-header')
}
window.addEventListener('scroll', scrollHeader)
/* ============================ scroll section active link ============================ */
const sections = document.querySelectorAll('section[id]')
let sectionId;
function scrollActive(){
const scrollY = window.pageYOffset
sections.forEach(current =>{
const sectionHeight = current.offsetHeight
const sectionTop = current.offsetTop - 50;
sectionId = current.getAttribute('id')
if(scrollY > sectionTop && scrollY <= sectionTop + sectionHeight){
document.querySelector('.nav-item a[href*=' + sectionId + ']').classList.add('active')
}else{
document.querySelector('.nav-item a[href*=' + sectionId + ']').classList.remove('active')
}
})
}
window.addEventListener('scroll', scrollActive)