forked from 11dd/11dd.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
133 lines (116 loc) · 4.47 KB
/
Copy pathscript.js
File metadata and controls
133 lines (116 loc) · 4.47 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const navToggle = document.querySelector(".nav-toggle");
const navLinks = document.querySelector(".nav-links");
const navAnchors = Array.from(document.querySelectorAll(".nav-links a"));
const sections = navAnchors
.map((anchor) => {
const href = anchor.getAttribute("href") || "";
if (!href.startsWith("#")) return null;
return document.querySelector(href);
})
.filter(Boolean);
navToggle?.addEventListener("click", () => {
const expanded = navToggle.getAttribute("aria-expanded") === "true";
navToggle.setAttribute("aria-expanded", String(!expanded));
navLinks?.classList.toggle("open");
});
navAnchors.forEach((anchor) => {
anchor.addEventListener("click", () => {
navLinks?.classList.remove("open");
navToggle?.setAttribute("aria-expanded", "false");
});
});
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (!entry.isIntersecting) return;
navAnchors.forEach((anchor) => {
anchor.classList.toggle("active", anchor.getAttribute("href") === `#${entry.target.id}`);
});
});
},
{
rootMargin: "-35% 0px -55% 0px",
threshold: 0,
}
);
sections.forEach((section) => observer.observe(section));
document.querySelectorAll(".filter-button").forEach((button) => {
button.addEventListener("click", () => {
const filter = button.dataset.filter;
document.querySelectorAll(".filter-button").forEach((item) => item.classList.remove("active"));
button.classList.add("active");
document.querySelectorAll(".defect-card").forEach((card) => {
const shouldShow = filter === "all" || card.dataset.category === filter;
card.classList.toggle("is-hidden", !shouldShow);
});
});
});
document.querySelectorAll(".accordion-trigger").forEach((trigger) => {
trigger.addEventListener("click", () => {
const panel = trigger.nextElementSibling;
const isOpen = trigger.classList.contains("active");
document.querySelectorAll(".accordion-trigger").forEach((item) => item.classList.remove("active"));
document.querySelectorAll(".accordion-panel").forEach((item) => item.classList.remove("open"));
if (!isOpen) {
trigger.classList.add("active");
panel?.classList.add("open");
}
});
});
document.querySelectorAll(".param-tab").forEach((tab) => {
tab.addEventListener("click", () => {
document.querySelectorAll(".param-tab").forEach((item) => item.classList.remove("active"));
document.querySelectorAll(".param-panel").forEach((item) => item.classList.remove("active"));
tab.classList.add("active");
document.querySelector(`[data-panel="${tab.dataset.param}"]`)?.classList.add("active");
});
});
(function () {
const storageKey = "vi-theme";
const root = document.documentElement;
const btn = document.getElementById("theme-toggle");
const label = btn?.querySelector(".theme-toggle-label");
function applyTheme(theme) {
if (theme === "light") root.setAttribute("data-theme", "light");
else root.removeAttribute("data-theme");
try {
localStorage.setItem(storageKey, theme === "light" ? "light" : "dark");
} catch (_) {}
if (btn && label) {
if (theme === "light") {
btn.setAttribute("aria-label", "切换为黑夜模式");
label.textContent = "黑夜";
} else {
btn.setAttribute("aria-label", "切换为白天模式");
label.textContent = "白天";
}
}
}
applyTheme(root.getAttribute("data-theme") === "light" ? "light" : "dark");
btn?.addEventListener("click", () => {
applyTheme(root.getAttribute("data-theme") === "light" ? "dark" : "light");
});
})();
/** 窄屏与顶栏「菜单」同布局时强制收起主题按钮(避免 z-index 盖住菜单) */
(function () {
const btn = document.getElementById("theme-toggle");
if (!btn) return;
const mq = window.matchMedia("(max-width: 1180px)");
function syncCompactNavChrome() {
if (mq.matches) {
btn.style.setProperty("display", "none", "important");
btn.style.setProperty("visibility", "hidden", "important");
btn.style.setProperty("pointer-events", "none", "important");
btn.setAttribute("aria-hidden", "true");
btn.setAttribute("tabindex", "-1");
} else {
btn.style.removeProperty("display");
btn.style.removeProperty("visibility");
btn.style.removeProperty("pointer-events");
btn.removeAttribute("aria-hidden");
btn.removeAttribute("tabindex");
}
}
mq.addEventListener("change", syncCompactNavChrome);
syncCompactNavChrome();
})();