The answer from @Miguel already has an example of how to do, so I will just show the code of the site that was referred to in the question.
How about we "steal" their source?
They use jQuery and anchors to make such functionality, as can be seen in the code below:
(function ($) {
//Add current view's highlighting to the navigation
/** helper for highlighting */
function highlightNav(navLinks,id)
{
navLinks.filter('[href="/#'+id+'"]').addClass("active");
}
$(window).scroll(function() {
//console.log("They see me scrollin, they hatin");
//clear highlighting
var navLinks = $('.site-navigation a');
navLinks.removeClass("active");
//calc current viewport
var viewTop = $(window).scrollTop();
var viewBottom = viewTop + $(window).height();
//for all h1 and h2 elements, check if they are visible
//performance tweak: stop each() after the first element is found to be behind view
var previous = "";
var foundOne = false;
var fallback = "";
$('h1, h2').each(function(i,e) {
//get element position;
var eTop = $(e).offset().top;
var eBottom = eTop + $(e).height();
var id=e.id;
id = id.replace("_title", "");
if (eTop >= viewTop) {
//if we are passed the view and no heading was highlighted yet, store previous one as fallback
if (! foundOne) {
fallback=previous;
}
if (eBottom <= viewBottom) {
highlightNav(navLinks, id);
foundOne = true;
} else {
return false; //break the each(), the rest is below
}
}
previous=id;
});
//no h1/h2 is in the viewport, so highlight the last one above
if (! foundOne) {
highlightNav(navLinks, fallback);
}
});
})(jQuery);
That code looks for the anchor that’s on scroll
and adds the class active
in the menu item.
If you want to study further, this code can be found on straight past.
Note that I did not find the origin of the code to post the copyright, so I posted what was found on the site cited in the question and Google searches.
Your "joke" answers the question. Just bring to the answer, not to depend on the link... Will one day the internet explode right? kkk
– Randrade
It was just to n get very extensive. I will post the answer
– Miguel
I added your code to the snippet, I hope it’s OK. p
– Randrade