Skip to content

Instantly share code, notes, and snippets.

@qwersk
Last active May 18, 2017 02:53
Show Gist options
  • Save qwersk/1761a632ea9a23b39fc41542cec30347 to your computer and use it in GitHub Desktop.
Save qwersk/1761a632ea9a23b39fc41542cec30347 to your computer and use it in GitHub Desktop.
ACTIVE MENU ITEM ON SCROLL #JS #JAVASCRIPT #JQUERY
var sections = $('.content section')
, nav = $('.header-top')
, nav_height = nav.outerHeight();
$(window).on('scroll', function () {
var cur_pos = $(this).scrollTop();
sections.each(function() {
var top = $(this).offset().top - nav_height - 25,
bottom = top + $(this).outerHeight();
if (cur_pos >= top && cur_pos <= bottom) {
nav.find('a').removeClass('active');
//sections.removeClass('active');
//console.log('active');
//$(this).addClass('active');
nav.find('a[href="/#'+$(this).attr('id')+'"]').addClass('active');
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment