document.addEventListener('DOMContentLoaded', () => { // --- 配置区域 --- // 设定今年的年份,方便计算 const currentYear = new Date().getFullYear(); // 考试日期配置 (月-日) const GAOKAO_DATE = `${currentYear}-06-07`; const ZHONGKAO_DATE = `${currentYear}-06-20`; // 假设中考在6月20日,可按需修改 // 节假日配置 (简化版,实际项目可接入API) // 格式:'MM-DD': { name: '节日名', type: 'rest'/'work' } const holidays = { '01-01': { name: '元旦', type: 'rest' }, '05-01': { name: '劳动节', type: 'rest' }, '10-01': { name: '国庆节', type: 'rest' }, // 可以在这里添加更多... }; // --- DOM 元素获取 --- const els = { clock: document.getElementById('clock'), date: document.getElementById('dateDisplay'), status: document.getElementById('restStatus'), holiday: document.getElementById('holidayName'), zhongkaoBox: document.getElementById('zhongkaoInfo'), zhongkaoCount: document.getElementById('zhongkaoCountdown'), gaokaoBox: document.getElementById('gaokaoFixedDate'), // 获取高考强制显示框 weatherIcon: document.getElementById('weatherIcon'), weatherText: document.getElementById('weatherText'), body: document.body }; // --- 核心功能函数 --- // 1. 更新时间与日期 function updateTime() { const now = new Date(); els.clock.textContent = now.toLocaleTimeString('en-GB'); // 24小时制 els.date.textContent = now.toLocaleDateString('zh-CN', { year: 'numeric', month: 'long', day: 'numeric', weekday: 'long' }); checkExamAndHoliday(now); } // 2. 检查考试与节假日逻辑 function checkExamAndHoliday(now) { const month = String(now.getMonth() + 1).padStart(2, '0'); const day = String(now.getDate()).padStart(2, '0'); const todayStr = `${month}-${day}`; // A. 处理节假日/工作日状态 if (holidays[todayStr]) { const info = holidays[todayStr]; els.status.textContent = info.type === 'rest' ? '今日休息' : '今日调休上班'; els.status.className = `badge ${info.type}`; els.holiday.textContent = info.name; } else { // 简单的周末判断 const dayOfWeek = now.getDay(); if (dayOfWeek === 0 || dayOfWeek === 6) { els.status.textContent = '周末愉快'; els.status.className = 'badge rest'; els.holiday.textContent = ''; } else { els.status.textContent = '努力工作'; els.status.className = 'badge work'; els.holiday.textContent = ''; } } // B. 处理中考倒计时 const zhongkaoTarget = new Date(ZHONGKAO_DATE); const diffTime = zhongkaoTarget - now; const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); if (diffDays > 0 && diffDays < 365) { els.zhongkaoBox.classList.remove('hidden'); els.zhongkaoCount.textContent = `${diffDays} 天`; } else { els.zhongkaoBox.classList.add('hidden'); } // C. 处理高考强制日期显示 (无论何时都显示,或者你可以加个判断只在考前显示) // 这里设置为:只要还没过6月9日,就一直显示,或者全年显示 // 现在的逻辑是:全年强制显示在中考下面 els.gaokaoBox.classList.remove('hidden'); } // 3. 模拟天气效果 (随机切换背景) function updateWeather() { const weathers = [ { type: 'sunny', icon: '☀️', text: '晴朗', class: 'bg-sunny' }, { type: 'cloudy', icon: '☁️', text: '多云', class: 'bg-cloudy' }, { type: 'rainy', icon: '🌧️', text: '小雨', class: 'bg-rainy' } ]; // 简单随机,实际可接API const w = weathers[Math.floor(Math.random() * weathers.length)]; els.weatherIcon.textContent = w.icon; els.weatherText.textContent = w.text; // 清除旧背景类,添加新背景类 els.body.className = ''; els.body.classList.add(w.class); } // --- 初始化运行 --- setInterval(updateTime, 1000); updateTime(); // 立即执行一次 updateWeather(); // 加载时随机一个天气 });