Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dannyconnolly/5739c1af6e694503a268 to your computer and use it in GitHub Desktop.
Save dannyconnolly/5739c1af6e694503a268 to your computer and use it in GitHub Desktop.
Adds class to the current section that is visible on screen for one page website
var sections = $('.section')
, nav = $('.menu-main-navigation-container')
, 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');
}
});
});
nav.find('a').on('click', function(e) {
e.preventDefault();
var $el = $(this);
var id = $el.attr('href');
var pos = $(id).offset().top;
$('html,body').stop().animate({
scrollTop: pos
}, 1500, 'easeOutSine');
//return false;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment