Created
February 6, 2018 13:00
-
-
Save shirokoweb/0bcf784298fbb510ad131f8e0dc22266 to your computer and use it in GitHub Desktop.
Add active
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
(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