I'am trying to makes my navbar moves between sections will I scroll down.
I tried to makes it with JS but I cant makes it work
it is change the active class when I click with event listener but I can't make it work when I scroll
Thanks for any help
here is my js code
let section = document.querySelectorAll('section')
let lists = document.querySelectorAll('.list');
function activeLink(){
lists.forEach((item) =>
item.classList.remove('active'));
this.classList.add('active');
}
lists.forEach((item) =>
item.addEventListener('click',activeLink));
window.onscroll = () =>{
section.forEach(sec =>{
let top = window.scrollY;
let offset = sec.offsetTop;
let height = sec.offsetHeight;
let id = sec.getAttribute('id');
if(top >= offset && top < offset + height){
lists.forEach(sec =>{
activeLink;
})
}
})
};
and this is my html
<nav class="navigation">
<ul>
<li class="list active">
<a href="#home">
<span class="icon">
<ion-icon name="home-outline"></ion-icon>
</span>
<span class="title">Home</span>
</a>
</li>
<li class="list">
<a href="#about">
<span class="icon">
<ion-icon name="person-outline"></ion-icon>
</span>
<span class="title">About Me</span>
</a>
</li>
<li class="list">
<a href="#working">
<span class="icon">
<ion-icon name="newspaper-outline"></ion-icon>
</span>
<span class="title">Working</span>
</a>
</li>
<li class="list">
<a href="#">
<span class="icon">
<ion-icon name="code-slash-outline"></ion-icon>
</span>
<span class="title">Learned</span>
</a>
</li>
<li class="list">
<a href="#contact">
<span class="icon">
<ion-icon name="chatbox-outline"></ion-icon>
</span>
<span class="title">Contact</span>
</a>
</li>
</ul>
</nav>