移除懒加载

This commit is contained in:
FlintyLemming
2026-01-19 12:03:19 +08:00
parent aa91183919
commit 9d657b0e5a
3 changed files with 18 additions and 46 deletions

View File

@@ -1,14 +1,19 @@
// Implements a scroll spy system for the ToC, displaying the current section with an indicator and scrolling to it when needed.
// Inspired from https://gomakethings.com/debouncing-your-javascript-events/
function debounced(func: Function) {
let timeout;
return () => {
if (timeout) {
window.cancelAnimationFrame(timeout);
}
// Throttle function - limits execution to once per frame, with trailing call
function throttled(func: Function) {
let ticking = false;
let lastArgs: any = null;
timeout = window.requestAnimationFrame(() => func());
return (...args: any[]) => {
lastArgs = args;
if (!ticking) {
ticking = true;
window.requestAnimationFrame(() => {
func.apply(null, lastArgs);
ticking = false;
});
}
}
}
@@ -74,8 +79,8 @@ function setupScrollspy() {
// We need to avoid scrolling when the user is actively interacting with the ToC. Otherwise, if the user clicks on a link in the ToC,
// we would scroll their view, which is not optimal usability-wise.
let tocHovered: boolean = false;
scrollableNavigation.addEventListener("mouseenter", debounced(() => tocHovered = true));
scrollableNavigation.addEventListener("mouseleave", debounced(() => tocHovered = false));
scrollableNavigation.addEventListener("mouseenter", () => tocHovered = true, { passive: true });
scrollableNavigation.addEventListener("mouseleave", () => tocHovered = false, { passive: true });
let activeSectionLink: Element;
@@ -119,7 +124,7 @@ function setupScrollspy() {
}
}
window.addEventListener("scroll", debounced(scrollHandler));
window.addEventListener("scroll", throttled(scrollHandler), { passive: true });
// Resizing may cause the offset values to change: recompute them.
function resizeHandler() {
@@ -130,11 +135,11 @@ function setupScrollspy() {
// Use ResizeObserver to detect changes in the size of .article-content
const articleContent = document.querySelector(".article-content");
if (articleContent) {
const resizeObserver = new ResizeObserver(debounced(resizeHandler));
const resizeObserver = new ResizeObserver(throttled(resizeHandler));
resizeObserver.observe(articleContent);
}
window.addEventListener("resize", debounced(resizeHandler));
window.addEventListener("resize", throttled(resizeHandler), { passive: true });
}
export { setupScrollspy };