Skip to content

Instantly share code, notes, and snippets.

@shehabkhan013
Created December 17, 2017 20:17
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 shehabkhan013/7fd3e03b28d30a8ccde92706199cffa2 to your computer and use it in GitHub Desktop.
Save shehabkhan013/7fd3e03b28d30a8ccde92706199cffa2 to your computer and use it in GitHub Desktop.
//Nav menu active class change
var lastId
, topMenu = $(".menu")
, topMenuHeight = topMenu.outerHeight() + 15
,menuItems = topMenu.find("a")
,scrollItems = menuItems.map(function () {
var item = $($(this).attr("href"));
if (item.length) {
return item;
}
});
// Bind to scroll
$(window).scroll(function () {
var fromTop = $(this).scrollTop() + topMenuHeight;
var cur = scrollItems.map(function () {
if ($(this).offset().top < fromTop) return this;
});
cur = cur[cur.length - 1];
var id = cur && cur.length ? cur[0].id : "";
if (lastId !== id) {
lastId = id;
menuItems.parent().removeClass("active").end().filter("[href='#" + id + "']").parent().addClass("active");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment