“MediaWiki:Common.js”的版本间的差异

来自部落与弯刀Wiki

第1行: 第1行:
 
/* 这里的任何JavaScript将为所有用户在每次页面载入时加载。 */
 
/* 这里的任何JavaScript将为所有用户在每次页面载入时加载。 */
document.addEventListener("DOMContentLoaded", function () {
+
(function () {
     const tocLinks = Array.from(document.querySelectorAll("#toc a"));
+
     function initTocHighlighter() {
    const sections = tocLinks.map(link => {
+
        const tocLinks = Array.from(document.querySelectorAll("#toc a"));
        const hash = link.getAttribute("href").slice(1);
+
        if (tocLinks.length === 0) {
        const target = document.getElementById(hash) || document.querySelector(`[id="${CSS.escape(hash)}"]`);
+
            console.warn("TOC 高亮:未找到目录链接");
        return target ? { link, target } : null;
+
            return;
    }).filter(Boolean);
+
        }
 +
 
 +
        const sections = tocLinks.map(link => {
 +
            const rawId = link.getAttribute("href").slice(1); // 去除 # 前缀
 +
            const target = document.getElementById(rawId) || document.querySelector(`[id="${CSS.escape(rawId)}"]`);
 +
            return target ? { link, target } : null;
 +
        }).filter(Boolean);
 +
 
 +
        if (sections.length === 0) {
 +
            console.warn("TOC 高亮:未能匹配任何章节");
 +
        }
  
    function updateHighlight() {
+
        function updateHighlight() {
        const scrollY = window.scrollY + 120;
+
            const scrollY = window.scrollY + 120;
        let current = null;
+
            let current = null;
 +
 
 +
            for (let section of sections) {
 +
                if (section.target.offsetTop <= scrollY) {
 +
                    current = section;
 +
                } else {
 +
                    break;
 +
                }
 +
            }
  
        for (let section of sections) {
+
            tocLinks.forEach(link => link.classList.remove("active-section"));
             if (section.target.offsetTop <= scrollY) {
+
             if (current) {
                 current = section;
+
                 current.link.classList.add("active-section");
            } else {
+
                 console.log("当前章节:", current.link.textContent);
                 break;
 
 
             }
 
             }
 
         }
 
         }
  
         tocLinks.forEach(l => l.classList.remove("active-section"));
+
         window.addEventListener("scroll", updateHighlight);
         if (current) current.link.classList.add("active-section");
+
         updateHighlight();
 +
        console.log("📘 TOC 高亮脚本已初始化,监测段落数:", sections.length);
 
     }
 
     }
  
     updateHighlight();
+
     // 加载完成后执行:保险起见用双触发机制
     window.addEventListener("scroll", updateHighlight);
+
     document.addEventListener("DOMContentLoaded", () => {
});
+
        requestAnimationFrame(() => {
 +
            initTocHighlighter();
 +
        });
 +
    });
 +
})();

2025年6月10日 (二) 18:34的版本

/* 这里的任何JavaScript将为所有用户在每次页面载入时加载。 */
(function () {
    function initTocHighlighter() {
        const tocLinks = Array.from(document.querySelectorAll("#toc a"));
        if (tocLinks.length === 0) {
            console.warn("TOC 高亮:未找到目录链接");
            return;
        }

        const sections = tocLinks.map(link => {
            const rawId = link.getAttribute("href").slice(1); // 去除 # 前缀
            const target = document.getElementById(rawId) || document.querySelector(`[id="${CSS.escape(rawId)}"]`);
            return target ? { link, target } : null;
        }).filter(Boolean);

        if (sections.length === 0) {
            console.warn("TOC 高亮:未能匹配任何章节");
        }

        function updateHighlight() {
            const scrollY = window.scrollY + 120;
            let current = null;

            for (let section of sections) {
                if (section.target.offsetTop <= scrollY) {
                    current = section;
                } else {
                    break;
                }
            }

            tocLinks.forEach(link => link.classList.remove("active-section"));
            if (current) {
                current.link.classList.add("active-section");
                console.log("当前章节:", current.link.textContent);
            }
        }

        window.addEventListener("scroll", updateHighlight);
        updateHighlight();
        console.log("📘 TOC 高亮脚本已初始化,监测段落数:", sections.length);
    }

    // 加载完成后执行:保险起见用双触发机制
    document.addEventListener("DOMContentLoaded", () => {
        requestAnimationFrame(() => {
            initTocHighlighter();
        });
    });
})();