i'm using this code to highlight the navbar when scrolling into sections. Is it possible to change the URL in the address bar instantly when I scroll to an section? Can anyone help me?
Thanks in advance
const [current, setCurrent] = useState('')
const sections = document.querySelectorAll('h3')
const navLi = document.querySelectorAll('nav ul li')
window.addEventListener('scroll', () => {
sections.forEach(section => {
const sectionTop = section.offsetTop
if (window.pageYOffset >= sectionTop && (window.pageYOffset + 100) - sectionTop < 200 && current !== section.getAttribute('id')) {
setCurrent(section.getAttribute('id'))
}
})
})
useEffect(() => {
navLi.forEach(li => {
li.classList.remove('test')
if(li.classList.contains(current)) {
li.classList.add('test')
}
})
}, [current])