User:Bhsd/widget/countdown.js
< User:Bhsd | widget
Bhsd(讨论 | 贡献)2020年11月30日 (一) 01:16的版本 (Bhsd移动页面User:Bhsd/countdown.js至User:Bhsd/widget/countdown.js,不留重定向:错误命名)
注意:在保存之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。
- Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
- Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
- Internet Explorer:按住Ctrl的同时单击刷新,或按Ctrl-F5
- Opera:前往菜单 → 设置(Mac为Opera → Preferences),然后隐私和安全 → 清除浏览数据 → 缓存的图片和文件。
//<nowiki> // 用于[[Widget:Countdown]],可以使用ES6语法 "use strict"; (function() { const fromNow = function (ele) { const now = new Date(), then = new Date( ele.dataset.target ), before = ele.children[0], after = ele.children[1], i18n = ele.dataset.title, textMonth = i18n.slice(2, 4).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>'), textHour = i18n.slice(0, 2).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>'), isBefore = (then < now), monthsHave31Days = [0, 2, 4, 6, 7, 9, 11]; // 月份从0开始 let year = isBefore ? now.getFullYear() - then.getFullYear() : then.getFullYear() - now.getFullYear(), month = isBefore ? now.getMonth() - then.getMonth() : then.getMonth() - now.getMonth(), day = isBefore ? now.getDate() - then.getDate() : then.getDate() - now.getDate(), hour = isBefore ? now.getHours() - then.getHours() : then.getHours() - now.getHours(), minute = isBefore ? now.getMinutes() - then.getMinutes() : then.getMinutes() - now.getMinutes(), second = isBefore ? now.getSeconds() - then.getSeconds() : then.getSeconds() - now.getSeconds(), result = ""; if (second < 0) { minute--; second += 60; } if (minute < 0) { hour--; minute += 60; } if (hour < 0) { day--; hour += 24; } if (day < 0) { month--; if (monthsHave31Days.includes( (isBefore ? then : now).getMonth() )) { day += 31; } else if ((isBefore ? then : now).getMonth() === 1) { if ((isBefore ? then : now).getFullYear() % 4 === 0) { day += 29; } else { day += 28; } } else { day += 30; } } if (month < 0) { year--; month += 12; } if (year > 0) { result += `<span class="countdown-num">${year}</span>年`; } if (month > 0 || result !== "") { result += `<span class="countdown-num">${month}</span>${textMonth}`; } if (day > 0 || result !== "") { result += `<span class="countdown-num">${day}</span>天`; } if (year === 0 && month === 0) { if (hour > 0 || result !== "") { result += `<span class="countdown-num">${hour}</span>${textHour}`; } if (day === 0) { if (minute > 0 || result !== "") { result += `<span class="countdown-num">${minute}</span>分`; } if (second > 0 || result !== "") { result += `<span class="countdown-num">${second}</span>秒`; } } } if (isBefore) { const countdown = before.querySelector( '.countdown' ); if (countdown) { countdown.innerHTML = result; } before.style.display = ""; after.style.display = "none"; } else { const countdown = after.querySelector( '.countdown' ); if (countdown) { countdown.innerHTML = result; } after.style.display = ""; before.style.display = "none"; } }, run = function() { document.querySelectorAll( '.countdownNode' ).forEach(ele => { if (ele.title) { ele.dataset.title = ele.title; ele.removeAttribute('title'); } fromNow(ele); ele.style.visibility = "visible"; }); }; document.querySelectorAll( '.countdownNode' ).forEach(ele => { // 补齐2位数日期 const target = ele.dataset.target.replace( /-(\d)(?=\D)/g, '-0$1' ).replace( /-(\d)$/, '-0$1' ), time = new Date(target); if (!time.getTime()) { ele.classList.add( "error" ); ele.textContent = "(时间格式错误!)"; ele.classList.remove( 'countdownNode' ); ele.removeAttribute('title'); } else { ele.dataset.target = target; } }); run(); window.setInterval(run, 1000); }) (); //</nowiki> // [[category:JavaScript小部件]] {{DEFAULTSORT:Countdown}}