增加暗色模式切换

This commit is contained in:
Alex Yang
2026-01-28 11:20:00 +08:00
parent a5dc5841fb
commit 2e85bbe7c7
10 changed files with 6491 additions and 209 deletions

View File

@@ -371,6 +371,42 @@ function setupNavigation() {
});
}
// 主题切换功能
function setupThemeToggle() {
const themeToggle = document.getElementById('theme-toggle');
const body = document.body;
if (!themeToggle) return;
// 从localStorage加载主题偏好
const savedTheme = localStorage.getItem('theme');
if (savedTheme) {
body.classList.toggle('dark', savedTheme === 'dark');
updateThemeIcon(themeToggle, savedTheme === 'dark');
}
// 添加点击事件监听器
themeToggle.addEventListener('click', () => {
const isDark = body.classList.toggle('dark');
updateThemeIcon(themeToggle, isDark);
// 保存主题偏好到localStorage
localStorage.setItem('theme', isDark ? 'dark' : 'light');
});
}
// 更新主题图标
function updateThemeIcon(toggleElement, isDark) {
const icon = toggleElement.querySelector('i');
if (icon) {
if (isDark) {
icon.className = 'fa fa-sun-o text-sm sm:text-lg';
} else {
icon.className = 'fa fa-moon-o text-sm sm:text-lg';
}
}
}
// 初始化函数
function init() {
// 设置导航
@@ -379,6 +415,9 @@ function init() {
// 设置账户功能
setupAccountFeatures();
// 设置主题切换
setupThemeToggle();
// 初始化页面
initPageByHash();