打开/关闭菜单
60
73
29
1.1K
武外梗百科
打开/关闭外观设置菜单
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。

MediaWiki:Common.js:修订间差异

MediaWiki界面页面
无编辑摘要
无编辑摘要
 
(未显示同一用户的5个中间版本)
第24行: 第24行:
`);
`);


mw.loader.using('mediawiki.util').then(function() {
mw.loader.using(['mediawiki.util', 'mediawiki.api']).then(function () {
     document.addEventListener('DOMContentLoaded', function () {
     $(function () {
         var el = document.getElementById('wiki-wordcount-number');
         var $el = $('#wiki-wordcount-number');
         if (!el) return;
         if (!$el.length) return;


         new mw.Api().get({
         // 标记是否已触发过动画
            action: 'query',
         var triggered = false;
            meta: 'siteinfo',
            siprop: 'statistics',
            format: 'json'
         }).done(function (data) {
            try {
                var stats = data.query.statistics;
                var total = stats['cirrussearch-article-words'] || 0;


                // 动画
        // IntersectionObserver 配置
                var current = 0;
        var observer = new IntersectionObserver(function(entries) {
                 var target = total;
            entries.forEach(function(entry) {
                var steps = 120;
                 if (entry.isIntersecting && !triggered) {
                var step = Math.ceil(target / steps);
                    triggered = true;
                 if (step < 1) step = 1;
                    startCountAnimation();
                    observer.unobserve(entry.target);
                 }
            });
        }, {
            root: null,      // 视口为浏览器窗口
            rootMargin: "0px",
            threshold: 0.3  // 至少 30% 可见才触发
        });


                 var interval = setInterval(function () {
        // 观察目标元素
                     current += step;
        observer.observe(document.getElementById('wiki-wordcount-number'));
                     if (current >= target) {
 
                        current = target;
        // 真正执行字数滚动动画
                         clearInterval(interval);
        function startCountAnimation() {
            new mw.Api().get({
                action: 'wordcounter',
                prop: 'totals',
                format: 'json'
            }).done(function (data) {
                // --- 修改点:适配您的 API 返回结构 ---
                 var total = 0;
                if (data && data.wordcounter && data.wordcounter.totals) {
                    total = data.wordcounter.totals.totalWords || 0;
                }
 
                // 缓动函数(前快后慢)
                function easeOutQuad(t) {
                     return t * (2 - t);
                }
 
                var duration = 2000;
                var startTime = null;
 
                function animate(timestamp) {
                     if (!startTime) startTime = timestamp;
                    var run = timestamp - startTime;
                    var progress = Math.min(run / duration, 1);
                    var eased = easeOutQuad(progress);
 
                    var currentVal = Math.floor(eased * total);
                    $el.text(currentVal.toLocaleString());
 
                    if (progress < 1) {
                         requestAnimationFrame(animate);
                     }
                     }
                    el.textContent = current.toLocaleString();
                 }
                 }, 15);
                 requestAnimationFrame(animate);
 
             }).fail(function () {
            } catch (e) {
                console.error('字数统计 API 请求失败');
                 console.error("字数统计解析错误", e);
            });
             }
        }
        }).fail(function () {
            console.error("字数统计 API 请求失败");
        });
     });
     });
});
});