diff options
| author | Reorx <novoreorx@gmail.com> | 2022-04-10 17:47:41 +0800 |
|---|---|---|
| committer | Reorx <novoreorx@gmail.com> | 2022-04-10 21:33:27 +0800 |
| commit | d7f6d6a7734b658b28c543b403810d397bbb6070 (patch) | |
| tree | 68188c5f1aee2303113dbe0a0d3086e2295a337d | |
| parent | d50ce4575db054c1873e659c57a7ff1636b067e5 (diff) | |
use setTimeout to reduce scroll callback frequency
| -rw-r--r-- | layouts/partials/footer.html | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/layouts/partials/footer.html b/layouts/partials/footer.html index 9415aaa..a9d9d7b 100644 --- a/layouts/partials/footer.html +++ b/layouts/partials/footer.html @@ -146,7 +146,7 @@ window.addEventListener('DOMContentLoaded', function (event) { let activeHeading = headings[0]; getLinkByHeading(activeHeading).classList.add(activeClass); - window.addEventListener('scroll', () => { + const onScroll = () => { const passedHeadings = []; for (const h of headings) { // 5 px as a buffer @@ -166,6 +166,14 @@ window.addEventListener('DOMContentLoaded', function (event) { activeHeading = newActiveHeading; getLinkByHeading(activeHeading).classList.add(activeClass); } + } + + let timer = null; + window.addEventListener('scroll', () => { + if (timer !== null) { + clearTimeout(timer) + } + timer = setTimeout(onScroll, 50) }, false); function getLinkByHeading(heading) { |
