(function($) { // Cache selectors var lastId, topMenu = $(".main-nav"), topMenuHeight = topMenu.outerHeight()+15, // All list items menuItems = topMenu.find(".awsm-go-to"), // Anchors corresponding to menu items scrollItems = menuItems.map(function(){ var item = $($(this).attr("href")); if (item.length) { return item; } }); // Bind to scroll $(window).scroll(function(){ // Get container scroll position var fromTop = $(this).scrollTop()+topMenuHeight; // Get id of current scroll item var cur = scrollItems.map(function(){ if ($(this).offset().top < fromTop) return this; }); // Get the id of the current element cur = cur[cur.length-1]; var id = cur && cur.length ? cur[0].id : ""; if (lastId !== id) { lastId = id; // Set/remove active class menuItems .parent().removeClass("active") .end().filter("[href='#"+id+"']").parent().addClass("active"); } }); $(document).ready(function(){ $('.awsm-go-to').click(function(event) { var $anchor = $(this); $('html, body').stop().animate({ scrollTop: $($anchor.attr('href')).offset().top }, 1000); event.preventDefault(); if ($(window).width() < 830) { $('.nav-toggle').removeClass('open'); $('.main-nav').slideUp(); } }); $('.nav-toggle').click(function(e){ e.preventDefault(); $(this).toggleClass('open'); $('.main-nav').slideToggle(); }); }); })(jQuery);