Skip to content

Instantly share code, notes, and snippets.

@nogginbox
Created January 11, 2021 14:26
Show Gist options
  • Save nogginbox/d250dbdc3629cd3fe98d114b7939a889 to your computer and use it in GitHub Desktop.
Save nogginbox/d250dbdc3629cd3fe98d114b7939a889 to your computer and use it in GitHub Desktop.
Highlight active section link
$(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