From 7373030687d563a37e560108e4786d32b861cbdd Mon Sep 17 00:00:00 2001 From: Dan Allen Date: Thu, 9 Apr 2020 04:10:15 -0600 Subject: [PATCH] slighly tweak the thresholds for the scroll spy --- src/js/02-on-this-page.js | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/js/02-on-this-page.js b/src/js/02-on-this-page.js index 9d24937..c568555 100644 --- a/src/js/02-on-this-page.js +++ b/src/js/02-on-this-page.js @@ -55,17 +55,13 @@ var buffer = getStyleValueAsInt(document.documentElement, 'fontSize') * 1.15 var targetPosition = article.offsetTop if (scrolledBy && window.innerHeight + scrolledBy + 2 >= document.documentElement.scrollHeight) { - if (lastActiveFragment) { - if (!Array.isArray(lastActiveFragment)) lastActiveFragment = [lastActiveFragment] - } else { - lastActiveFragment = [] - } + lastActiveFragment = Array.isArray(lastActiveFragment) ? lastActiveFragment : Array(lastActiveFragment || 0) var activeFragments = [] var lastIdx = headings.length - 1 headings.forEach(function (heading, idx) { var fragment = '#' + heading.id if (idx === lastIdx || - heading.getBoundingClientRect().top + getStyleValueAsInt(heading, 'paddingTop') >= targetPosition) { + heading.getBoundingClientRect().top + getStyleValueAsInt(heading, 'paddingTop') > targetPosition) { activeFragments.push(fragment) if (lastActiveFragment.indexOf(fragment) < 0) links[fragment].classList.add('is-active') } else if (~lastActiveFragment.indexOf(fragment)) { @@ -84,7 +80,7 @@ } var activeFragment headings.some(function (heading) { - if (targetPosition < heading.getBoundingClientRect().top + getStyleValueAsInt(heading, 'paddingTop') - buffer) { + if (heading.getBoundingClientRect().top + getStyleValueAsInt(heading, 'paddingTop') - buffer > targetPosition) { return true } activeFragment = '#' + heading.id -- 2.11.0