diff options
| author | Reorx <novoreorx@gmail.com> | 2022-04-13 00:28:44 +0800 |
|---|---|---|
| committer | Reorx <novoreorx@gmail.com> | 2022-04-13 00:29:43 +0800 |
| commit | 1019c47b7cd822d7b3ae9423dbc5fd3c38d82b45 (patch) | |
| tree | b6a3539027dd41e9912cc2668aabda6960d11651 | |
| parent | d4b3ef754f8f34342726b8f9f20c40b502706ddc (diff) | |
makes toc scroll plays well with InstantClick
by adding/removing scroll event listener each time page is loaded
| -rw-r--r-- | layouts/partials/footer.html | 30 |
1 files changed, 23 insertions, 7 deletions
diff --git a/layouts/partials/footer.html b/layouts/partials/footer.html index ab4496c..52e80fe 100644 --- a/layouts/partials/footer.html +++ b/layouts/partials/footer.html @@ -138,12 +138,26 @@ </script> {{- end }} -{{- if (and (eq .Kind "page") (.Content) (.Param "ShowToc") (.Param "TocSide")) }} + <script> -window.addEventListener('DOMContentLoaded', function (event) { - if (!document.querySelector('.toc')) { - return; +// NOTE use closure instead of DOMContentLoaded because of InstantClick, +// every page loaded by InstantClick should run the <script> tags again. +(function() { + const enableTocScroll = '{{- if (and (eq .Kind "page") (.Content) (.Param "ShowToc") (.Param "TocSide")) }}1{{ end }}' == '1' + let scrollListeners = window.scrollListeners + if (scrollListeners === undefined) { + scrollListeners = [] + window.scrollListeners = scrollListeners + } + + if (!enableTocScroll || !document.querySelector('.toc')) { + // console.log('disable toc scroll', scrollListeners) + for (const listener of scrollListeners) { + window.removeEventListener('scroll', listener) + } + return } + const headings = document.querySelectorAll('h1[id],h2[id],h3[id],h4[id],h5[id]'); const activeClass = 'active'; @@ -174,12 +188,14 @@ window.addEventListener('DOMContentLoaded', function (event) { } let timer = null; - window.addEventListener('scroll', () => { + const scrollListener = () => { if (timer !== null) { clearTimeout(timer) } timer = setTimeout(onScroll, 50) - }, false); + } + window.addEventListener('scroll', scrollListener, false); + scrollListeners.push(scrollListener) function getLinkByHeading(heading) { const id = encodeURI(heading.getAttribute('id')).toLowerCase(); @@ -193,7 +209,7 @@ window.addEventListener('DOMContentLoaded', function (event) { let rect = heading.getBoundingClientRect(); return rect.top } -}, false); +})(); </script> {{- end }} |
