-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
36 lines (34 loc) · 1.23 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
/*For active menu
var menu = document.getElementById("navbar");
var items = menu.getElementsByClassName("items");
for(var i = 0 ; i < items.length ; i++) {
items[i].addEventListener("click", function(){
var active = document.getElementsByClassName("active");
active[0].className = active[0].className.replace(" active", "");
this.className += " active";
});
}*/
//console.log("Hello");
const sections = document.querySelectorAll('section');
//console.log(sections);
const navLiA = document.querySelectorAll('nav ul li a');
window.addEventListener('scroll', ()=>{
let current = "";
//console.log(pageYOffset);
sections.forEach( section => {
const sectionTop = section.offsetTop;
const sectionHeight = section.clientHeight;
// console.log(sectionTop);
if(pageYOffset >= (sectionTop) - sectionHeight/3) {
current = section.getAttribute('id');
}
})
//console.log(current);
navLiA.forEach( a =>{
a.classList.remove('active');
if(a.classList.contains(current)){
//console.log(current);
a.classList.add('active');
}
})
})