Skip to content

Instantly share code, notes, and snippets.

@davidchc
Created April 2, 2018 02:13
Show Gist options
  • Save davidchc/72324b9342f825bc338a2900b9e200d2 to your computer and use it in GitHub Desktop.
Save davidchc/72324b9342f825bc338a2900b9e200d2 to your computer and use it in GitHub Desktop.
jQuery(function(){
var items = [];
var key = 0;
var nav = jQuery(".itens-topo a");
nav.each(function(){
var target = jQuery(this).attr('href');
var section = jQuery(target);
if(section.length) {
var topSection = section.offset().top;
var heightSection = topSection + section.height();
items.push({id : target, start: topSection, end : heightSection, index : key});
}
key++;
});
jQuery(window).scroll(function(){
var currentScroll = jQuery(document).scrollTop();
items.forEach(function(item){
if(item.start <= currentScroll && item.end > currentScroll) {
nav.removeClass('active')
nav.eq(item.index).addClass('active');
}
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment