Created
March 29, 2015 21:57
-
-
Save tillsanders/b13bb620f838494782c3 to your computer and use it in GitHub Desktop.
Update active nav item while scrolling
This file contains 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
$(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