网站纪念日自动灰屏实现方案
网站纪念日自动灰屏实现方案
该方法通过配置清明节等纪念日日期,在对应日期自动为网站首页应用 CSS 灰度滤镜。包含移除旧样式、首页检测、纪念日判断等步骤,并兼容 Pjax 页面切换,确保灰屏效果正确显示。
function getQingmingDate() { const today = new Date(); const year = today.getFullYear(); const Y = year % 100; const C = year >= 2000 ? 4.81 : 5.59; const L = Math.floor(Y / 4); const qingming = Math.floor(Y * 0.2422 + C) - L; return { month: 4, day: Math.max(4, qingming) }; }
const memorialDays = [ { month: 12, day: 13 }, getQingmingDate(), { month: 7, day: 7 }, { month: 9, day: 18 }, { month: 9, day: 30 }, ];
function updateMourningMode() { document.documentElement.classList.remove('mourning-mode'); const homePaths = ['/', '/index.html', '/index.htm', '/index']; const isHomePage = homePaths.includes(window.location.pathname); if (!isHomePage) return; const today = new Date(); const currentMonth = today.getMonth() + 1; const currentDay = today.getDate(); const isMemorialDay = memorialDays.some(day => day.month === currentMonth && day.day === currentDay ); if (isMemorialDay) { document.documentElement.classList.add('mourning-mode'); } }
const style = document.createElement('style'); style.textContent = ` html.mourning-mode { filter: grayscale(100%); -webkit-filter: grayscale(100%); transition: filter 0.5s ease; } `; document.head.appendChild(style);
updateMourningMode(); document.addEventListener('pjax:complete', updateMourningMode);
|