/* ============================================================ common.js — 全站公共脚本 包含: 主题初始化、滚动进度条、退出登录、GSAP 入场动画、 formatBytes 工具函数 ============================================================ */ /* ── 1. 主题初始化 & 切换(优先读 localStorage,否则跟随系统)── */ (function initTheme() { const root = document.documentElement; const systemDark = window.matchMedia('(prefers-color-scheme: dark)'); function applyTheme(isDark) { root.classList.toggle('dark', isDark); root.classList.toggle('light', !isDark); // 同步主题图标(如果页面有 #themeIcon) const icon = document.getElementById('themeIcon'); if (icon) { icon.innerHTML = isDark ? '' /* 月亮 */ : ''; /* 太阳 */ } } // 首次加载:localStorage > 系统偏好 const saved = localStorage.getItem('theme'); if (saved === 'dark' || saved === 'light') { applyTheme(saved === 'dark'); } else { applyTheme(systemDark.matches); } // 系统切换时,仅在没有手动记忆时跟随 systemDark.addEventListener('change', function (e) { if (!localStorage.getItem('theme')) { applyTheme(e.matches); } }); })(); /* ── 全局主题切换(供所有页面 toggleTheme() 调用)──────────── */ function toggleTheme() { const isDark = document.documentElement.classList.contains('dark'); const next = !isDark; document.documentElement.classList.toggle('dark', next); document.documentElement.classList.toggle('light', !next); localStorage.setItem('theme', next ? 'dark' : 'light'); // 同步图标 const icon = document.getElementById('themeIcon'); if (icon) { icon.innerHTML = next ? '' : ''; } } /* ── 2. 滚动进度条(页面需要有 id="scrollProgress" 元素)──── */ window.addEventListener('scroll', function () { const el = document.getElementById('scrollProgress'); if (!el) return; const winScroll = document.body.scrollTop || document.documentElement.scrollTop; const height = document.documentElement.scrollHeight - document.documentElement.clientHeight; const scrolled = height > 0 ? (winScroll / height) * 100 : 0; el.style.width = scrolled + '%'; }); /* ── 3. 退出登录 ──────────────────────────────────────────── */ async function logout() { try { await fetch('/api/logout', { method: 'POST' }); window.location.href = '/login'; } catch (err) { if (typeof showToast === 'function') { showToast('退出失败', 'error'); } } } /* ── 4. GSAP 入场动画(页面需要有 id="headerSection" 和 .stagger-item 元素,且 GSAP 已加载)──────────────── */ function initPageAnimation() { if (typeof gsap === 'undefined') return; const header = document.getElementById('headerSection'); if (header) { gsap.to(header, { opacity: 1, y: 0, duration: 0.8, ease: 'power2.out', delay: 0.1 }); } const items = document.querySelectorAll('.stagger-item'); if (items.length > 0) { gsap.to(items, { opacity: 1, y: 0, duration: 0.6, stagger: 0.1, ease: 'power2.out', delay: 0.3 }); } } /* ── 5. formatBytes — 字节数格式化工具 ───────────────────── */ function formatBytes(bytes, decimals) { decimals = decimals !== undefined ? decimals : 2; if (!bytes || bytes === 0) return '0 B'; var k = 1024; var sizes = ['B', 'KB', 'MB', 'GB', 'TB']; var i = Math.floor(Math.log(bytes) / Math.log(k)); i = Math.min(i, sizes.length - 1); return parseFloat((bytes / Math.pow(k, i)).toFixed(decimals)) + ' ' + sizes[i]; } /* ── 6. Tailwind 统一配置(在 tailwind.config 未定义时注入)─ */ /* 注意:各页面仍需自行在 tailwind.config 中声明 darkMode: 'class' 本函数仅作为兼容性保底,不替代页面内的 tailwind.config 声明 */