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

来自部落与弯刀Wiki

(创建页面,内容为“→‎这里的任何JavaScript将为所有用户在每次页面载入时加载。 document.addEventListener("DOMContentLoaded", function () { const tocLinks = A…”
 
第3行: 第3行:
 
     const tocLinks = Array.from(document.querySelectorAll("#toc a"));
 
     const tocLinks = Array.from(document.querySelectorAll("#toc a"));
 
     const sections = tocLinks.map(link => {
 
     const sections = tocLinks.map(link => {
         const target = document.getElementById(decodeURIComponent(link.getAttribute("href").substring(1)));
+
         const id = decodeURIComponent(link.getAttribute("href").slice(1));
         return target ? { link, target } : null;
+
        const el = document.getElementById(id);
 +
         if (el) {
 +
            return { link: link, target: el };
 +
        }
 +
        return null;
 
     }).filter(Boolean);
 
     }).filter(Boolean);
  
     function onScroll() {
+
     function updateHighlight() {
        let currentSection = null;
+
         const scrollPosition = window.scrollY + 120;
         const scrollTop = window.scrollY + 100; // 提前一点触发高亮
 
  
         for (let i = 0; i < sections.length; i++) {
+
         let current = null;
             const sectionTop = sections[i].target.offsetTop;
+
        for (const section of sections) {
            if (scrollTop >= sectionTop) {
+
             if (section.target.offsetTop <= scrollPosition) {
                 currentSection = sections[i];
+
                 current = section;
 
             } else {
 
             } else {
 
                 break;
 
                 break;
第20行: 第23行:
 
         }
 
         }
  
 +
        // 清除所有高亮
 
         tocLinks.forEach(link => link.classList.remove("active-section"));
 
         tocLinks.forEach(link => link.classList.remove("active-section"));
         if (currentSection) {
+
 
             currentSection.link.classList.add("active-section");
+
        // 添加当前高亮
 +
         if (current) {
 +
             current.link.classList.add("active-section");
 
         }
 
         }
 
     }
 
     }
  
     window.addEventListener("scroll", onScroll);
+
    // 首次触发 + 滚动触发
    onScroll(); // 初始加载时触发一次
+
    updateHighlight();
 +
     window.addEventListener("scroll", updateHighlight);
 
});
 
});

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

/* 这里的任何JavaScript将为所有用户在每次页面载入时加载。 */
document.addEventListener("DOMContentLoaded", function () {
    const tocLinks = Array.from(document.querySelectorAll("#toc a"));
    const sections = tocLinks.map(link => {
        const id = decodeURIComponent(link.getAttribute("href").slice(1));
        const el = document.getElementById(id);
        if (el) {
            return { link: link, target: el };
        }
        return null;
    }).filter(Boolean);

    function updateHighlight() {
        const scrollPosition = window.scrollY + 120;

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

        // 清除所有高亮
        tocLinks.forEach(link => link.classList.remove("active-section"));

        // 添加当前高亮
        if (current) {
            current.link.classList.add("active-section");
        }
    }

    // 首次触发 + 滚动触发
    updateHighlight();
    window.addEventListener("scroll", updateHighlight);
});