Skip to content

Commit

Permalink
Update ToC to highlight last header if page is scrolled to very bottom,
Browse files Browse the repository at this point in the history
fixes #280
  • Loading branch information
lord committed Feb 24, 2017
1 parent 2158f8b commit 53ff31a
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions source/javascripts/app/_toc.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@

function loadToc($toc, tocLinkSelector, tocListSelector, scrollOffset) {
var headerHeights = {};
var pageHeight = 0;
var windowHeight = 0;

var recacheHeights = function() {
headerHeights = {};
pageHeight = $(document).height();
windowHeight = $(window).height();

$toc.find(tocLinkSelector).each(function() {
var targetId = $(this).attr('href');
Expand All @@ -33,6 +37,13 @@
var refreshToc = function() {
var currentTop = $(document).scrollTop() + scrollOffset;

if (currentTop + windowHeight >= pageHeight) {
// at bottom of page, so just select last header by making currentTop very large
// this fixes the problem where the last header won't ever show as active if its content
// is shorter than the window height
currentTop = pageHeight + 1000;
}

var best = null;
for (var name in headerHeights) {
if ((headerHeights[name] < currentTop && headerHeights[name] > headerHeights[best]) || best === null) {
Expand Down

0 comments on commit 53ff31a

Please sign in to comment.