Skip to content

Instantly share code, notes, and snippets.

@ylkyrg
Created October 8, 2018 13:21
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 ylkyrg/a21f7b2869408e59a45fbadb8524419b to your computer and use it in GitHub Desktop.
Save ylkyrg/a21f7b2869408e59a45fbadb8524419b to your computer and use it in GitHub Desktop.
Active class for jump nav by id
(function($) {
$(window).scroll(function() {
var scroll = $(window).scrollTop();
// Active class for jump nav
$("section").each(function(i) {
var id = $(this).attr("id");
if ($(this).offset().top <= scroll + 200) {
$(".navbar .nav-item").removeClass("active");
$("a[href='/#" + id + "']")
.parent()
.addClass("active");
} else {
$("a[href='/#" + id + "']")
.parent()
.removeClass("active");
}
});
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment