返回 jQuery 实例与特效代码列表
文字与动效MIT兼容 jQuery 1.12 / 3.8 / 4.1
数字滚动增长
将关键业务指标从 0 平滑递增到目标值并自动格式化千分位,常用于数据大屏与官网成果展示。
兼容性说明:使用 requestAnimationFrame 驱动,全版本兼容(含 slim 精简版)。
在线交互演示(独立沙箱)
$(function () {
function runCount() {
$('.cu-num').each(function () {
var $el = $(this);
var target = Number($el.data('target'));
var dec = Number($el.data('dec') || 0);
var start = performance.now(), dur = 900;
function frame(now) {
var p = Math.min(1, (now - start) / dur);
var eased = 1 - Math.pow(1 - p, 3);
var val = target * eased;
$el.text(dec ? val.toFixed(dec) : Math.round(val).toLocaleString());
if (p < 1) requestAnimationFrame(frame);
}
requestAnimationFrame(frame);
});
}
$('#cu-replay').on('click', runCount);
runCount();
});不用 jQuery 怎么写?(现代原生 JS / CSS 替代思路)
使用 requestAnimationFrame 按缓动函数计算进度,通过 toLocaleString() 格式化输出。
const step = now => {
const p = Math.min(1, (now - start) / duration);
el.textContent = Math.round(target * (1 - Math.pow(1 - p, 3))).toLocaleString();
if (p < 1) requestAnimationFrame(step);
};