Widget:Countdown:修订间差异
来自OGAS数据中枢
更多操作
无编辑摘要 |
无编辑摘要 |
||
| (未显示同一用户的3个中间版本) | |||
| 第1行: | 第1行: | ||
<includeonly> | <includeonly> | ||
<!--{assign var="targetTime" value=$time|default:$t}--> | |||
<span class="custom-countdown" | <span class="custom-countdown" | ||
data-target="<!--{$ | data-target="<!--{$targetTime|escape:'quotes'}-->" | ||
data-before="<!--{$before|default:'$1前'}-->" | data-before="<!--{$before|default:'$1前'|escape:'quotes'}-->" | ||
data-after="<!--{$after|default:'还剩$1'}-->">...</span> | data-after="<!--{$after|default:'还剩$1'|escape:'quotes'}-->">...</span> | ||
<script> | <script> | ||
(function() { | (function() { | ||
let | let timer = null; | ||
function | function update() { | ||
const now = new Date(); | const now = new Date(); | ||
document.querySelectorAll('.custom-countdown').forEach(el => { | document.querySelectorAll('.custom-countdown').forEach(el => { | ||
let targetStr = el.getAttribute('data-target'); | |||
const beforeTpl = el.getAttribute('data-before'); | const beforeTpl = el.getAttribute('data-before'); | ||
const afterTpl = el.getAttribute('data-after'); | const afterTpl = el.getAttribute('data-after'); | ||
// | if (!targetStr) return; | ||
if (/^\d{4}$/.test(targetStr)) { | |||
const target = new Date( | targetStr += "-01-01T00:00:00"; | ||
} | |||
if (/[+-]\d{2}$/.test(targetStr)) { | |||
targetStr += ":00"; | |||
} | |||
if (/T\d{2}([+-]|Z|$)/.test(targetStr)) { | |||
targetStr = targetStr.replace(/(T\d{2})/, "$1:00:00"); | |||
} | |||
if (targetStr === 'today-end') { | |||
targetStr = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 23, 59, 59).toISOString(); | |||
} | |||
const target = new Date(targetStr); | |||
if (isNaN(target)) { | if (isNaN(target)) { | ||
el.innerHTML = '<span style="color:red">时间格式错误</span>'; | el.innerHTML = '<span style="color:red">时间格式错误 (' + targetStr + ')</span>'; | ||
return; | return; | ||
} | } | ||
| 第27行: | 第39行: | ||
const absDiff = Math.abs(diff); | const absDiff = Math.abs(diff); | ||
const days = Math.floor(absDiff / 864e5); | const days = Math.floor(absDiff / 864e5); | ||
const hours = Math.floor((absDiff % 864e5) / 36e5); | const hours = Math.floor((absDiff % 864e5) / 36e5); | ||
| 第39行: | 第50行: | ||
diffStr += secs + "秒"; | diffStr += secs + "秒"; | ||
if (diff > 0) { | if (diff > 0) { | ||
el.innerHTML = afterTpl.replace('$1', diffStr); | el.innerHTML = afterTpl.replace('$1', diffStr); | ||
| 第48行: | 第58行: | ||
} | } | ||
function start() { if(! | function start() { if(!timer){ update(); timer=setInterval(update, 1000); } } | ||
function stop() { clearInterval( | function stop() { clearInterval(timer); timer=null; } | ||
document.addEventListener('visibilitychange', () => { document.hidden ? stop() : start(); }); | document.addEventListener('visibilitychange', () => { document.hidden ? stop() : start(); }); | ||
start(); | start(); | ||
2026年5月3日 (日) 15:32的最新版本
这是一个倒计时微件,仅供{{Countdown}}使用。