I have a single page website with the navigation menu position:fixed at the top of the page.
When I click a link from the navigation menu the page scrolls to the appropriate section using this JQuery:
$('a[href^="#"]').live('click',function(event){
event.preventDefault();
var target_offset = $(this.hash).offset() ? $(this.hash).offset().top : 0;
$('html, body').animate({scrollTop:target_offset}, 1200, 'easeOutExpo');
});
What I’d like to happen is when I manually scroll the page $(window).scroll(function(){...});, relevant to the section passing under the navigation menu #navi-container, the navigation link highlights using .addClass('activeNav');
Check-out this jsfiddle I stumbled across a few days ago, I believe it’s just what you’re looking for: http://jsfiddle.net/x3V6Y/
I would like to note that if you end-up using this that you re-think the
for(i in sections)loop as it is a big hit to performance. If you can, it is an excellent idea to use this kind of loop:…but that requires a re-think of how to store the offsets of the section elements since this type of loop requires an array rather than an object (an object will work but it has to be zero-indexed and all the indexes have to be integers).