Skip to content

Instantly share code, notes, and snippets.

@tillsanders
Created March 29, 2015 21:57
Show Gist options
  • Save tillsanders/b13bb620f838494782c3 to your computer and use it in GitHub Desktop.
Save tillsanders/b13bb620f838494782c3 to your computer and use it in GitHub Desktop.
Update active nav item while scrolling
$(window).scroll(function() {
var current = $('main section').map(function(){
if($(this).offset().top < $(window).scrollTop() + $(window).height() / 2) {
return $(this);
}
});
current = current.last().get(0);
$('nav.main a').removeClass('active');
$('a[href=#' + current.attr('id') + ']').addClass('active');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment