Skip to content

Instantly share code, notes, and snippets.

@shirokoweb
Created February 6, 2018 13:00
Show Gist options
  • Save shirokoweb/0bcf784298fbb510ad131f8e0dc22266 to your computer and use it in GitHub Desktop.
Save shirokoweb/0bcf784298fbb510ad131f8e0dc22266 to your computer and use it in GitHub Desktop.
Add active
(function($) {
$(document).ready(function() {
var navChildren = $("#top-menu li").children();
var aArray = [];
for (var i = 0; i < navChildren.length; i++) {
var aChild = navChildren[i];
// var ahref = $(aChild).attr('href');
var ahrefTMP = $(aChild).attr('href');
var ahref = ahrefTMP.substring(ahrefTMP.indexOf('#'));
aArray.push(ahref);
}
$(window).scroll(function() {
var windowPos = $(window).scrollTop();
var windowHeight = $(window).height();
var docHeight = $(document).height();
for (var i = 0; i < aArray.length; i++) {
var theID = aArray[i];
var secPosition = $(theID).offset().top;
secPosition = secPosition - 135;
var divHeight = $(theID).height();
divHeight = divHeight + 90;
if (windowPos >= secPosition && windowPos < (secPosition + divHeight)) {
$("a[href='" + theID + "']").parent().addClass("active");
} else {
$("a[href='" + theID + "']").parent().removeClass("active");
}
}
});
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment