// Highlight Menu Item on Scroll
var lastId;
var topMenu = $("#desktop-nav");
var topMenuHeight = topMenu.outerHeight();
var menuItems = topMenu.find("a");
scrollItems = menuItems.map(function() {
var item = $($(this).attr("href"));
if (item.length) {
return item;
}
});
menuItems.click(function(e){
var href = $(this).attr("href");
var offsetTop = href === "#" ? 0 : $(href).offset().top-topMenuHeight+1;
$('html, body').stop().animate({
scrollTop: offsetTop
}, 300);
e.preventDefault();
});
$(window).scroll(function(){
var fromTop = $(this).scrollTop()+topMenuHeight;
var current = scrollItems.map(function(){
if ($(this).offset().top <= fromTop)
return this;
});
current = current[current.length-1];
var id = current && current.length ? current[0].id : "";
if (lastId !== id) {
lastId = id;
// Set/remove active class
menuItems
.parent().removeClass("active")
.end().filter("[href='#"+id+"']").parent().addClass("active");
}
});
One Page Scroll Nav Without Plugin
----------------------------------
A simple JQuery script to create a one page navigation and scrolling effect. User can click the navigation to jump to the specific section/page.
A [Pen](https://codepen.io/jksakura/pen/LkzAXz) by [Jake Zhong](https://codepen.io/jksakura) on [CodePen](https://codepen.io).
[License](https://codepen.io/jksakura/pen/LkzAXz/license).