Last active
August 29, 2015 14:03
-
-
Save dannyconnolly/5739c1af6e694503a268 to your computer and use it in GitHub Desktop.
Adds class to the current section that is visible on screen for one page website
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sections = $('.section') | |
, nav = $('.menu-main-navigation-container') | |
, nav_height = nav.outerHeight(); | |
$(window).on('scroll', function() { | |
var cur_pos = $(this).scrollTop(); | |
sections.each(function() { | |
var top = $(this).offset().top - nav_height, | |
bottom = top + $(this).outerHeight(); | |
if (cur_pos >= top && cur_pos <= bottom) { | |
nav.find('a').removeClass('active'); | |
sections.removeClass('active'); | |
$(this).addClass('active'); | |
nav.find('a[href="#' + $(this).attr('id') + '"]').addClass('active'); | |
} | |
}); | |
}); | |
nav.find('a').on('click', function(e) { | |
e.preventDefault(); | |
var $el = $(this); | |
var id = $el.attr('href'); | |
var pos = $(id).offset().top; | |
$('html,body').stop().animate({ | |
scrollTop: pos | |
}, 1500, 'easeOutSine'); | |
//return false; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment