打开/关闭菜单
打开/关闭外观设置菜单
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。

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="<!--{$time|default:$1|escape:'quotes'}-->"  
       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 t = null;
     let timer = null;
     function u() {
     function update() {
         const now = new Date();
         const now = new Date();
         document.querySelectorAll('.custom-countdown').forEach(el => {
         document.querySelectorAll('.custom-countdown').forEach(el => {
             const targetStr = el.getAttribute('data-target');
             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');
              
 
             // 处理仅年份的情况,补全为 YYYY-01-01
             if (!targetStr) return;
             const finalTargetStr = /^\d{4}$/.test(targetStr) ? targetStr + "-01-01T00:00:00" : targetStr;
             if (/^\d{4}$/.test(targetStr)) {
             const target = new Date(finalTargetStr);
                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(!t){ u(); t=setInterval(u, 1000); } }
     function start() { if(!timer){ update(); timer=setInterval(update, 1000); } }
     function stop() { clearInterval(t); t=null; }
     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}}使用。