use setTimeout to reduce scroll callback frequency
This commit is contained in:
parent
d50ce4575d
commit
d7f6d6a773
|
@ -146,7 +146,7 @@ window.addEventListener('DOMContentLoaded', function (event) {
|
||||||
let activeHeading = headings[0];
|
let activeHeading = headings[0];
|
||||||
getLinkByHeading(activeHeading).classList.add(activeClass);
|
getLinkByHeading(activeHeading).classList.add(activeClass);
|
||||||
|
|
||||||
window.addEventListener('scroll', () => {
|
const onScroll = () => {
|
||||||
const passedHeadings = [];
|
const passedHeadings = [];
|
||||||
for (const h of headings) {
|
for (const h of headings) {
|
||||||
// 5 px as a buffer
|
// 5 px as a buffer
|
||||||
|
@ -166,6 +166,14 @@ window.addEventListener('DOMContentLoaded', function (event) {
|
||||||
activeHeading = newActiveHeading;
|
activeHeading = newActiveHeading;
|
||||||
getLinkByHeading(activeHeading).classList.add(activeClass);
|
getLinkByHeading(activeHeading).classList.add(activeClass);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let timer = null;
|
||||||
|
window.addEventListener('scroll', () => {
|
||||||
|
if (timer !== null) {
|
||||||
|
clearTimeout(timer)
|
||||||
|
}
|
||||||
|
timer = setTimeout(onScroll, 50)
|
||||||
}, false);
|
}, false);
|
||||||
|
|
||||||
function getLinkByHeading(heading) {
|
function getLinkByHeading(heading) {
|
||||||
|
|
Loading…
Reference in New Issue