I'm converting this simple scrolling code from jQuery to vanilla Javascript but having small issues when selecting the elements. Can anyone point me in the right direction? Thanks a lot in advance!
Here's working jQuery code:
Now here's my vanilla Javascript code:
window.addEventListener('scroll', function() {
document.querySelectorAll('.target').forEach(function(item, index){
if($(window).scrollTop() >= $(this).offset().top) {
var id = $(this).attr('id');
document.querySelector('#nav nav a').classList.remove('active');
document.querySelector('#nav nav a[href=#'+ id +']').classList.add('active');
}
});
});
Vanilla JavaScript Demo: