fix: 动画改用 setInterval (兼容动态 script 加载) + try-catch 保护

This commit is contained in:
qinglong
2026-06-13 18:29:11 +08:00
parent c4b44e0fd2
commit a2bd6486a0
4 changed files with 39 additions and 15 deletions
+15 -13
View File
@@ -5,19 +5,21 @@ window.DashboardModule = {
charts: {},
_animateNumber: function(el, newVal, decimals, suffix) {
var self = this;
var oldVal = parseFloat(el.getAttribute('data-val') || newVal);
if (Math.abs(oldVal - newVal) < 0.05) { el.textContent = newVal.toFixed(decimals) + suffix; return; }
el.setAttribute('data-val', newVal);
var start = performance.now();
var duration = 900;
function step(ts) {
var p = Math.min((ts - start) / duration, 1);
var eased = 1 - Math.pow(1 - p, 3);
el.textContent = (oldVal + (newVal - oldVal) * eased).toFixed(decimals) + suffix;
if (p < 1) requestAnimationFrame(step);
}
requestAnimationFrame(step);
try {
var oldVal = parseFloat(el.getAttribute('data-val'));
if (isNaN(oldVal)) oldVal = newVal;
if (Math.abs(oldVal - newVal) < 0.05) { el.textContent = newVal.toFixed(decimals) + suffix; el.setAttribute('data-val', newVal); return; }
el.setAttribute('data-val', newVal);
var steps = 20, i = 0;
var stepMs = 50; // 20 steps × 50ms = 1000ms
var timer = setInterval(function(){
i++;
var p = Math.min(i / steps, 1);
var eased = 1 - Math.pow(1 - p, 3);
el.textContent = (oldVal + (newVal - oldVal) * eased).toFixed(decimals) + suffix;
if (i >= steps) clearInterval(timer);
}, stepMs);
} catch(e) { el.textContent = newVal.toFixed(decimals) + suffix; }
},
init: function() {