jquery(document).ready(function($){ var scrolling = false; var contentsections = $('.cd-section'), verticalnavigation = $('.cd-vertical-nav'), navigationitems = verticalnavigation.find('a'), navtrigger = $('.cd-nav-trigger'), scrollarrow = $('.cd-scroll-down'); $(window).on('scroll', checkscroll); //smooth scroll to the selected section verticalnavigation.on('click', 'a', function(event){ event.preventdefault(); smoothscroll($(this.hash)); verticalnavigation.removeclass('open'); }); //smooth scroll to the second section scrollarrow.on('click', function(event){ event.preventdefault(); smoothscroll($(this.hash)); }); // open navigation if user clicks the .cd-nav-trigger - small devices only navtrigger.on('click', function(event){ event.preventdefault(); verticalnavigation.toggleclass('open'); }); function checkscroll() { if( !scrolling ) { scrolling = true; (!window.requestanimationframe) ? settimeout(updatesections, 500) : window.requestanimationframe(updatesections); } } function updatesections() { var halfwindowheight = $(window).height()/2, scrolltop = $(window).scrolltop(); contentsections.each(function(){ var section = $(this), sectionid = section.attr('id'), navigationitem = navigationitems.filter('[href^="#'+ sectionid +'"]'); ( (section.offset().top - halfwindowheight < scrolltop ) && ( section.offset().top + section.height() - halfwindowheight > scrolltop) ) ? navigationitem.addclass('active') : navigationitem.removeclass('active'); }); scrolling = false; } function smoothscroll(target) { $('body,html').animate( {'scrolltop':target.offset().top}, 500 ); } });