Created
November 25, 2017 09:40
-
-
Save abhishek-9ithub/4ddb37da1a957eaa140c61c35af24543 to your computer and use it in GitHub Desktop.
sticky header on scroll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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