-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
61 lines (57 loc) · 1.76 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"use strict";
document.addEventListener("DOMContentLoaded", () => {
getSVGSprites();
summaryDeleteTitle();
toc();
});
//如果存在目录,添加目录交互
const toc = () => {
const tocContainer = document.getElementById("TableOfContents");
if (tocContainer !== null) {
const activeClass = "text-sky-400";
tocInit(activeClass);
tocScrolling(activeClass);
}
};
//首个目录项添加默认激活状态
const tocInit = (activeClass) => {
const tocContainer = document.getElementById("TableOfContents");
tocContainer.querySelector("li").classList.add(activeClass);
};
//处理滚动时的目录交互
const tocScrolling = (activeClass) => {
const sections = document.querySelectorAll("h2,h3");
window.onscroll = () => {
const scrollPos =
document.documentElement.scrollTop || document.body.scrollTop;
for (let section of sections) {
if (section.offsetTop <= scrollPos) {
const id = section.id;
const activeElement = document.querySelector(`.${activeClass}`);
activeElement?.classList.remove(activeClass);
document
.querySelector(`a[href*=${id}]`)
.parentNode.classList.add(activeClass);
}
}
};
};
//删除预览中的h2元素以降低对于预览格式的困扰
const summaryDeleteTitle = () => {
const summaries = document.querySelectorAll(".summary-item");
for (let summary of summaries) {
summary.querySelector("h2")?.remove();
}
};
//加载SVG Sprites
const getSVGSprites = () => {
let xhr = new XMLHttpRequest();
xhr.open("GET", "https://static.fori.fun/blog/theme/sprites.svg", true);
xhr.onload = () => {
document.body.insertAdjacentHTML(
"afterBegin",
'<div style="display:none;">' + xhr.responseText + "</div>"
);
};
xhr.send();
};