Highlight the menu link of the visible section
As the user scrolls, mark the link of the section that is currently in view.
jQuery · Highlight the menu link of the visible sectionIntermediate
const $links = $('#toc a');
const $sections = $links.map(function () { return $($(this).attr('href'))[0]; });
$(window).on('scroll', function () {
const pos = $(window).scrollTop() + 80;
let current = $sections.first().attr('id');
$sections.each(function () {
if ($(this).offset().top <= pos) current = this.id;
});
$links.removeClass('active').filter('[href="#' + current + '"]').addClass('active');
}).trigger('scroll');scroll spymenuscrollactive