Skip to content

Instantly share code, notes, and snippets.

@abhishek-9ithub
Created November 25, 2017 09:40
Show Gist options
  • Save abhishek-9ithub/4ddb37da1a957eaa140c61c35af24543 to your computer and use it in GitHub Desktop.
Save abhishek-9ithub/4ddb37da1a957eaa140c61c35af24543 to your computer and use it in GitHub Desktop.
sticky header on scroll
var sections = $('section')
, nav = $('nav')
, nav_height = nav.outerHeight();
$(window).on('scroll', function () {
var cur_pos = $(this).scrollTop();
sections.each(function() {
var top = $(this).offset().top - nav_height,
bottom = top + $(this).outerHeight();
if (cur_pos >= top && cur_pos <= bottom) {
nav.find('a').removeClass('active');
sections.removeClass('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