$(document).ready(function() { var sectionIds = $('a.nav-link'); $(document).scroll(function() { sectionIds.each(function() { var container = $(this).attr('href'); var containerOffset = $(container).offset().top; var containerHeight = $(container).outerHeight(); var containerBottom = containerOffset + containerHeight; var scrollPosition = $(document).scrollTop(); if (scrollPosition < containerBottom - 350 && scrollPosition >= containerOffset - 350) { $(this).addClass('active'); } else { $(this).removeClass('active'); } }); }); });
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter