mirror of
https://github.com/codeguy/php-the-right-way.git
synced 2025-08-07 06:26:45 +02:00
If no heading is in view, uses fallback
This commit is contained in:
@@ -26,33 +26,54 @@
|
|||||||
})(jQuery);
|
})(jQuery);
|
||||||
|
|
||||||
(function ($) {
|
(function ($) {
|
||||||
//Add current view's highlighting to the navigation
|
//Add current view's highlighting to the navigation
|
||||||
$(window).scroll(function() {
|
|
||||||
//console.log("They see me scrollin, they hatin");
|
/** helper for highlighting */
|
||||||
|
function highlightNav(navLinks,id)
|
||||||
|
{
|
||||||
|
navLinks.filter('[href="/#'+id+'"]').addClass("active");
|
||||||
|
}
|
||||||
|
|
||||||
//clear highlighting
|
$(window).scroll(function() {
|
||||||
var navLinks=$('.site-navigation a')
|
//console.log("They see me scrollin, they hatin");
|
||||||
navLinks.removeClass("active");
|
|
||||||
|
|
||||||
//calc current viewport
|
//clear highlighting
|
||||||
var viewTop = $(window).scrollTop();
|
var navLinks = $('.site-navigation a');
|
||||||
var viewBottom = viewTop + $(window).height();
|
navLinks.removeClass("active");
|
||||||
|
|
||||||
//for all h1 and h2 elements, check if they are visible
|
//calc current viewport
|
||||||
//performance tweak: stop each() after the first element is found to be behind view
|
var viewTop = $(window).scrollTop();
|
||||||
$('h1, h2').each(function(i,e) {
|
var viewBottom = viewTop + $(window).height();
|
||||||
//get element position;
|
|
||||||
var eTop = $(e).offset().top;
|
//for all h1 and h2 elements, check if they are visible
|
||||||
var eBottom = eTop + $(e).height();
|
//performance tweak: stop each() after the first element is found to be behind view
|
||||||
if (eTop >= viewTop) {
|
var previous = "";
|
||||||
if (eBottom <= viewBottom) {
|
var foundOne = false;
|
||||||
//filter cached navLinks
|
var fallback = "";
|
||||||
navLinks.filter('[href="/#'+e.id+'"]').addClass("active");
|
$('h1, h2').each(function(i,e) {
|
||||||
} else {
|
//get element position;
|
||||||
//console.log("Start skipping test with "+e.id);
|
var eTop = $(e).offset().top;
|
||||||
return false;
|
var eBottom = eTop + $(e).height();
|
||||||
}
|
var id=e.id;
|
||||||
}
|
|
||||||
});
|
if (eTop >= viewTop) {
|
||||||
});
|
//if we are passed the view and no heading was highlighted yet, store previous one as fallback
|
||||||
|
if (! foundOne) {
|
||||||
|
fallback=previous;
|
||||||
|
}
|
||||||
|
if (eBottom <= viewBottom) {
|
||||||
|
highlightNav(navLinks, id);
|
||||||
|
foundOne = true;
|
||||||
|
} else {
|
||||||
|
return false; //break the each(), the rest is below
|
||||||
|
}
|
||||||
|
}
|
||||||
|
previous=id;
|
||||||
|
});
|
||||||
|
//no h1/h2 is in the viewport, so highlight the last one above
|
||||||
|
if (! foundOne) {
|
||||||
|
highlightNav(navLinks, fallback);
|
||||||
|
}
|
||||||
|
});
|
||||||
})(jQuery);
|
})(jQuery);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user