Skip to content

Instantly share code, notes, and snippets.

@taniarascia
Created October 1, 2015 17:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taniarascia/b29626e3f2e8e8e9c3cb to your computer and use it in GitHub Desktop.
Save taniarascia/b29626e3f2e8e8e9c3cb to your computer and use it in GitHub Desktop.
Active Scroll
var sections = $('section'),
links = $('nav ul li a'),
lis = $('nav ul > li');
$(window).scroll(function() {
var currentPosition = $(this).scrollTop();
links.removeClass('active');
lis.removeClass('active');
sections.each(function() {
var top = $(this).offset().top,
bottom = top + $(this).height();
if (currentPosition >= top && currentPosition <= bottom) {
var link = $('a[href="#' + this.id + '"]');
link.addClass('active-color');
link.parent().addClass('active');
} else {
link = $('a[href="#' + this.id + '"]');
link.removeClass('active-color');
link.parent().removeClass('active');
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment