-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
157 lines (135 loc) · 5.27 KB
/
Copy pathscript.js
File metadata and controls
157 lines (135 loc) · 5.27 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const root = document.documentElement;
const themeToggle = document.querySelector('.theme-toggle');
const siteFavicon = document.querySelector('#site-favicon');
const menuToggle = document.querySelector('.menu-toggle');
const nav = document.querySelector('.main-nav');
const header = document.querySelector('.site-header');
const pageTitle = document.querySelector('#page-title');
const navTitle = document.querySelector('.nav-title');
const navSectionLinks = [...nav.querySelectorAll('a[href^="#"]')];
const navSections = navSectionLinks
.map((link) => ({ link, section: document.querySelector(link.getAttribute('href')) }))
.filter(({ section }) => section);
const systemTheme = window.matchMedia('(prefers-color-scheme: dark)');
const themeModes = ['auto', 'light', 'dark'];
const themeModeDetails = {
auto: { label: '自动', icon: '◐' },
light: { label: '浅色', icon: '☀' },
dark: { label: '深色', icon: '☾' },
};
function currentThemeMode() {
return themeModes.includes(root.dataset.themeMode) ? root.dataset.themeMode : 'auto';
}
function resolvedTheme(mode = currentThemeMode()) {
return mode === 'auto' ? (systemTheme.matches ? 'dark' : 'light') : mode;
}
function updateSiteFavicon(theme = resolvedTheme()) {
const themeHref = siteFavicon?.dataset[`${theme}Href`];
if (themeHref && siteFavicon.getAttribute('href') !== themeHref) {
siteFavicon.setAttribute('href', themeHref);
}
}
function applyThemeMode(mode, save = true) {
root.dataset.themeMode = mode;
if (mode === 'auto') {
delete root.dataset.theme;
} else {
root.dataset.theme = mode;
}
if (save) {
try { localStorage.setItem('dl-theme', mode); } catch (_) {}
}
updateSiteFavicon(resolvedTheme(mode));
updateThemeControl();
}
function updateThemeControl() {
const mode = currentThemeMode();
const currentIndex = themeModes.indexOf(mode);
const nextMode = themeModes[(currentIndex + 1) % themeModes.length];
const current = themeModeDetails[mode];
const next = themeModeDetails[nextMode];
const resolvedLabel = systemTheme.matches ? '深色' : '浅色';
const currentLabel = mode === 'auto' ? `自动(当前${resolvedLabel})` : current.label;
themeToggle.querySelector('.theme-icon').textContent = current.icon;
themeToggle.querySelector('.theme-mode-text').textContent = current.label;
themeToggle.dataset.mode = mode;
themeToggle.setAttribute('aria-label', `主题:${currentLabel};点击切换到${next.label}主题`);
themeToggle.setAttribute('title', `主题:${currentLabel}`);
}
themeToggle.addEventListener('click', () => {
const currentIndex = themeModes.indexOf(currentThemeMode());
const nextMode = themeModes[(currentIndex + 1) % themeModes.length];
applyThemeMode(nextMode);
});
function handleSystemThemeChange() {
updateSiteFavicon();
updateThemeControl();
}
if (typeof systemTheme.addEventListener === 'function') {
systemTheme.addEventListener('change', handleSystemThemeChange);
} else {
systemTheme.addListener(handleSystemThemeChange);
}
function closeMenu({ restoreFocus = false } = {}) {
const wasOpen = menuToggle.getAttribute('aria-expanded') === 'true';
nav.classList.remove('open');
menuToggle.setAttribute('aria-expanded', 'false');
menuToggle.setAttribute('aria-label', '打开导航菜单');
if (restoreFocus && wasOpen) menuToggle.focus();
}
menuToggle.addEventListener('click', () => {
const willOpen = menuToggle.getAttribute('aria-expanded') !== 'true';
menuToggle.setAttribute('aria-expanded', String(willOpen));
menuToggle.setAttribute('aria-label', willOpen ? '关闭导航菜单' : '打开导航菜单');
nav.classList.toggle('open', willOpen);
});
nav.addEventListener('click', (event) => {
if (event.target.closest('a')) {
closeMenu();
}
});
document.addEventListener('click', (event) => {
if (!nav.contains(event.target) && !menuToggle.contains(event.target)) {
closeMenu();
}
});
document.addEventListener('keydown', (event) => {
if (event.key === 'Escape') closeMenu({ restoreFocus: true });
});
let scrollFrame = 0;
function updateScrollState() {
header.classList.toggle('scrolled', window.scrollY > 8);
if (pageTitle && navTitle) {
const titleIsCovered = pageTitle.getBoundingClientRect().bottom <= header.getBoundingClientRect().bottom;
header.classList.toggle('title-visible', titleIsCovered);
}
const marker = window.scrollY + Math.min(window.innerHeight * 0.36, 280);
let activeId = null;
navSections.forEach(({ section }) => {
if (section.offsetTop <= marker) activeId = section.id;
});
navSections.forEach(({ link, section }) => {
const isActive = section.id === activeId;
link.classList.toggle('is-active', isActive);
if (isActive) {
link.setAttribute('aria-current', 'location');
} else {
link.removeAttribute('aria-current');
}
});
}
function scheduleScrollUpdate() {
if (scrollFrame) return;
scrollFrame = window.requestAnimationFrame(() => {
updateScrollState();
scrollFrame = 0;
});
}
window.addEventListener('scroll', scheduleScrollUpdate, { passive: true });
window.addEventListener('resize', () => {
if (window.innerWidth > 760) closeMenu();
scheduleScrollUpdate();
}, { passive: true });
document.querySelector('#year').textContent = new Date().getFullYear();
applyThemeMode(currentThemeMode(), false);
updateScrollState();