Skip to content

Commit

Permalink
Move navhighlight closure to setup.js
Browse files Browse the repository at this point in the history
Also limits the anchors manipulated to site-navigation
  • Loading branch information
xosofox committed Jul 19, 2012
1 parent d425cde commit e85082d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
28 changes: 0 additions & 28 deletions scripts/navhighlight.js

This file was deleted.

30 changes: 30 additions & 0 deletions scripts/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,33 @@
}).fail(fail);
}
})(jQuery);

(function ($) {
//Add current view's highlighting to the navigation
$(window).scroll(function() {
//console.log("They see me scrollin, they hatin");

//clear highlighting
$('.site-navigation a').removeClass("active");

//calc current viewport
var viewTop = $(window).scrollTop();
var viewBottom = viewTop + $(window).height();

//for all h1 and h2 elements, check if they are visible
//performance tweak: stop each() after the first element is found to be behind view
$('h1, h2').each(function(i,e) {
//get element position;
var eTop = $(e).offset().top;
var eBottom = eTop + $(e).height();
if (eTop >= viewTop) {
if (eBottom <= viewBottom) {
$('.site-navigation a[href="/#'+e.id+'"]').addClass("active");
} else {
//console.log("Start skipping test with "+e.id);
return false;
}
}
});
});
})(jQuery);

0 comments on commit e85082d

Please sign in to comment.