Created
January 11, 2021 14:26
-
-
Save nogginbox/d250dbdc3629cd3fe98d114b7939a889 to your computer and use it in GitHub Desktop.
Highlight active section link
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
$(document).ready(function () { | |
var $sections = $(".content-item"); | |
var scrollFunc = function () { | |
var scrollTop = $(this).scrollTop() + ((window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight) / 3); | |
var $activeSection = $($sections[0]); | |
for (var i = $sections.length - 1; i >= 0; i--) { | |
var $s = $($sections[i]); | |
var divPosition = $s.offset().top; | |
$activeSection = $s; | |
if (scrollTop > divPosition) { | |
console.info(i + ":" + divPosition + " < " + scrollTop); | |
break; | |
} | |
} | |
var id = $activeSection.find("h2").attr("id"); | |
$("a").removeClass("active"); | |
$('a[href="#' + id + '"]').addClass("active"); | |
}; | |
if ($sections) { | |
scrollFunc(); | |
$(window).scroll(scrollFunc); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment