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

来自部落与弯刀Wiki

 
(未显示同一用户的7个中间版本)
第1行: 第1行:
 
/* 这里的任何JavaScript将为所有用户在每次页面载入时加载。 */
 
/* 这里的任何JavaScript将为所有用户在每次页面载入时加载。 */
document.addEventListener("DOMContentLoaded", function () {
+
function initToc() {
    const tocLinks = Array.from(document.querySelectorAll("#toc a"));
+
var tocLinks = Array.prototype.slice.call(document.querySelectorAll("#toc a"));
    const sections = tocLinks.map(link => {
+
if (tocLinks.length === 0) {
        const hash = link.getAttribute("href").slice(1);
+
    console.warn("TOC 高亮:未找到目录链接");
        const target = document.getElementById(hash) || document.querySelector(`[id="${CSS.escape(hash)}"]`);
+
    return;
        return target ? { link, target } : null;
+
}
    }).filter(Boolean);
+
 +
var sections = tocLinks.map(function (link) {
 +
    var rawId = link.getAttribute("href").slice(1); // 去除 #
 +
    var target = document.getElementById(rawId) || document.querySelector('[id="' + CSS.escape(rawId) + '"]');
 +
    return target ? { link: link, target: target } : null;
 +
}).filter(function (s) { return s !== null; });
 +
 +
if (sections.length === 0) {
 +
    console.warn("TOC 高亮:未能匹配任何章节");
 +
}
 +
 +
function updateHighlight() {
 +
    var scrollY = window.scrollY + 120;
 +
    var current = null;
 +
 +
    for (var i = 0; i < sections.length; i++) {
 +
        if (sections[i].target.offsetTop <= scrollY) {
 +
            current = sections[i];
 +
        } else {
 +
            break;
 +
        }
 +
    }
 +
 +
    tocLinks.forEach(function (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);
 +
}
  
    function updateHighlight() {
+
initToc();
        const scrollY = window.scrollY + 120;
 
        let current = null;
 
 
 
        for (let section of sections) {
 
            if (section.target.offsetTop <= scrollY) {
 
                current = section;
 
            } else {
 
                break;
 
            }
 
        }
 
 
 
        tocLinks.forEach(l => l.classList.remove("active-section"));
 
        if (current) current.link.classList.add("active-section");
 
    }
 
 
 
    updateHighlight();
 
    window.addEventListener("scroll", updateHighlight);
 
});
 

2025年6月10日 (二) 18:57的最新版本

/* 这里的任何JavaScript将为所有用户在每次页面载入时加载。 */
function initToc() {
	var tocLinks = Array.prototype.slice.call(document.querySelectorAll("#toc a"));
	if (tocLinks.length === 0) {
	    console.warn("TOC 高亮:未找到目录链接");
	    return;
	}
	
	var sections = tocLinks.map(function (link) {
	    var rawId = link.getAttribute("href").slice(1); // 去除 #
	    var target = document.getElementById(rawId) || document.querySelector('[id="' + CSS.escape(rawId) + '"]');
	    return target ? { link: link, target: target } : null;
	}).filter(function (s) { return s !== null; });
	
	if (sections.length === 0) {
	    console.warn("TOC 高亮:未能匹配任何章节");
	}
	
	function updateHighlight() {
	    var scrollY = window.scrollY + 120;
	    var current = null;
	
	    for (var i = 0; i < sections.length; i++) {
	        if (sections[i].target.offsetTop <= scrollY) {
	            current = sections[i];
	        } else {
	            break;
	        }
	    }
	
	    tocLinks.forEach(function (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);
}

initToc();