diff options
| author | Reorx <novoreorx@gmail.com> | 2022-04-10 01:18:40 +0800 |
|---|---|---|
| committer | Reorx <novoreorx@gmail.com> | 2022-04-10 01:18:40 +0800 |
| commit | 9ebe2111f67c2684d3dcfa9f9e739890c7d7d54b (patch) | |
| tree | 3362226a12a8bf55398db038307c4909e439c96e /layouts | |
| parent | 60f139d529a00c90a75151912f3639060435a1d9 (diff) | |
active toc link while scrolling
Diffstat (limited to 'layouts')
| -rw-r--r-- | layouts/partials/footer.html | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/layouts/partials/footer.html b/layouts/partials/footer.html index d76058f..d533987 100644 --- a/layouts/partials/footer.html +++ b/layouts/partials/footer.html @@ -133,3 +133,52 @@ }); </script> {{- end }} + +{{- if (and (eq .Kind "page") (.Param "ShowToc") (.Param "TocSide")) }} +<script> +window.addEventListener('DOMContentLoaded', function (event) { + const headings = document.querySelectorAll('h1[id],h2[id],h3[id],h4[id],h5[id]'); + + // Make the first header active + let activeHeading = headings[0]; + function getLinkByHeading(heading) { + const id = encodeURI(heading.getAttribute('id')).toLowerCase(); + return document.querySelector(`.toc ul li a[href="#${id}"]`); + } + getLinkByHeading(activeHeading).classList.add('active'); + + window.addEventListener('scroll', () => { + const passedHeadings = []; + for (const h of headings) { + // 5 px as a buffer + if (getOffsetTop(h) < 5) { + passedHeadings.push(h) + } else { + break; + } + } + if (passedHeadings.length > 0) { + activeHeading = passedHeadings[passedHeadings.length - 1]; + } + + headings.forEach(heading => { + if (heading === activeHeading) { + getLinkByHeading(heading).classList.add('active'); + } else { + getLinkByHeading(heading).classList.remove('active'); + } + }) + }, false); + + function getOffsetTop(heading) { + if (!heading.getClientRects().length) { + return 0; + } + let rect = heading.getBoundingClientRect(); + return rect.top + // let win = heading.ownerDocument.defaultView; + // return rect.top + win.pageYOffset; + } +}, false); +</script> +{{- end }} |
