MediaWiki:Common.js
来自部落与弯刀Wiki
Skyswordkill(讨论 | 贡献)2025年6月10日 (二) 18:23的版本 (创建页面,内容为“→这里的任何JavaScript将为所有用户在每次页面载入时加载。: document.addEventListener("DOMContentLoaded", function () { const tocLinks = A…”)
注意:在保存之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。
- Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
- Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
- Internet Explorer:按住Ctrl的同时单击刷新,或按Ctrl-F5
- Opera:前往菜单 → 设置(Mac为Opera → Preferences),然后隐私和安全 → 清除浏览数据 → 缓存的图片和文件。
/* 这里的任何JavaScript将为所有用户在每次页面载入时加载。 */
document.addEventListener("DOMContentLoaded", function () {
const tocLinks = Array.from(document.querySelectorAll("#toc a"));
const sections = tocLinks.map(link => {
const target = document.getElementById(decodeURIComponent(link.getAttribute("href").substring(1)));
return target ? { link, target } : null;
}).filter(Boolean);
function onScroll() {
let currentSection = null;
const scrollTop = window.scrollY + 100; // 提前一点触发高亮
for (let i = 0; i < sections.length; i++) {
const sectionTop = sections[i].target.offsetTop;
if (scrollTop >= sectionTop) {
currentSection = sections[i];
} else {
break;
}
}
tocLinks.forEach(link => link.classList.remove("active-section"));
if (currentSection) {
currentSection.link.classList.add("active-section");
}
}
window.addEventListener("scroll", onScroll);
onScroll(); // 初始加载时触发一次
});