1
0
Fork 0
mirror of https://github.com/codex-team/codex.docs.git synced 2025-08-07 14:35:26 +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
*/
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
*/
@ -119,24 +126,14 @@ export default class TableOfContent {
/**
* Define a flag to reduce number of calls to detectSection function
*/
const throttledDetectSectionFunction = Decorators.throttle((lastKnownScrollPosition) => {
detectSection(lastKnownScrollPosition);
const throttledDetectSectionFunction = Decorators.throttle(() => {
detectSection();
}, 200);
/**
* Scroll listener
*/
document.addEventListener('scroll', (event) => {
/**
* Calculate scroll position
*/
let lastKnownScrollPosition = this.getScrollPadding() + window.scrollY + 1;
/**
* Call section detecting function
*/
throttledDetectSectionFunction(lastKnownScrollPosition);
});
document.addEventListener('scroll', throttledDetectSectionFunction);
}
/**