1
0
Fork 0
mirror of https://github.com/codex-team/codex.docs.git synced 2025-08-10 07:55:24 +02:00

Update table-of-content.js

This commit is contained in:
Taly 2022-07-20 11:57:47 +03:00
parent 4ff756e1d0
commit c740b97a26

View file

@ -93,7 +93,14 @@ export default class TableOfContent {
* Watch active section while scrolling * Watch active section while scrolling
*/ */
watchActiveSection() { watchActiveSection() {
const detectSection = (scrollPosition) => { const detectSection = () => {
/**
* Calculate scroll position
*
* @todo research how not to use magic number
*/
let scrollPosition = this.getScrollPadding() + window.scrollY + 1;
/** /**
* Find the nearest section above the scroll position * Find the nearest section above the scroll position
*/ */
@ -119,24 +126,14 @@ export default class TableOfContent {
/** /**
* Define a flag to reduce number of calls to detectSection function * Define a flag to reduce number of calls to detectSection function
*/ */
const throttledDetectSectionFunction = Decorators.throttle((lastKnownScrollPosition) => { const throttledDetectSectionFunction = Decorators.throttle(() => {
detectSection(lastKnownScrollPosition); detectSection();
}, 200); }, 200);
/** /**
* Scroll listener * Scroll listener
*/ */
document.addEventListener('scroll', (event) => { document.addEventListener('scroll', throttledDetectSectionFunction);
/**
* Calculate scroll position
*/
let lastKnownScrollPosition = this.getScrollPadding() + window.scrollY + 1;
/**
* Call section detecting function
*/
throttledDetectSectionFunction(lastKnownScrollPosition);
});
} }
/** /**