当前位置:首页 > JavaScript

js 实现数字滚动

2026-03-01 05:38:57JavaScript

js 实现数字滚动

数字滚动动画实现

数字滚动动画可以通过多种方式实现,以下是几种常见的实现方法:

使用 CSS 过渡和 JavaScript

function animateValue(id, start, end, duration) {
  const obj = document.getElementById(id);
  let startTimestamp = null;
  const step = (timestamp) => {
    if (!startTimestamp) startTimestamp = timestamp;
    const progress = Math.min((timestamp - startTimestamp) / duration, 1);
    obj.innerHTML = Math.floor(progress * (end - start) + start);
    if (progress < 1) {
      window.requestAnimationFrame(step);
    }
  };
  window.requestAnimationFrame(step);
}

// 使用示例
animateValue("counter", 0, 100, 2000);

使用 setInterval 实现

function countUp(elementId, target, duration) {
  const element = document.getElementById(elementId);
  const increment = target / (duration / 16);
  let current = 0;

  const timer = setInterval(() => {
    current += increment;
    if (current >= target) {
      clearInterval(timer);
      current = target;
    }
    element.textContent = Math.floor(current);
  }, 16);
}

// 使用示例
countUp("counter", 100, 2000);

使用 GSAP 动画库

// 引入 GSAP 库后
gsap.to("#counter", {
  innerHTML: 100,
  duration: 2,
  snap: { innerHTML: 1 },
  ease: "power1.out"
});

实现带小数点的滚动

function animateDecimal(id, start, end, duration, decimals = 0) {
  const element = document.getElementById(id);
  const range = end - start;
  const increment = end > start ? 1 : -1;
  const stepTime = Math.abs(Math.floor(duration / range));
  let current = start;

  const timer = setInterval(() => {
    current += increment;
    element.innerHTML = current.toFixed(decimals);
    if (current === end) {
      clearInterval(timer);
    }
  }, stepTime);
}

// 使用示例
animateDecimal("counter", 0, 99.99, 2000, 2);

实现带千位分隔符的数字滚动

function animateNumberWithCommas(id, start, end, duration) {
  const element = document.getElementById(id);
  const range = end - start;
  const minTimer = 50;
  const stepTime = Math.max(Math.floor(duration / range), minTimer);
  let current = start;

  const timer = setInterval(() => {
    current += 1;
    element.innerHTML = current.toLocaleString();
    if (current === end) {
      clearInterval(timer);
    }
  }, stepTime);
}

// 使用示例
animateNumberWithCommas("counter", 0, 10000, 2000);

实现缓动效果的滚动

function easeOutQuad(t, b, c, d) {
  t /= d;
  return -c * t*(t-2) + b;
}

function animateWithEasing(id, start, end, duration) {
  const element = document.getElementById(id);
  const startTime = performance.now();

  function updateNumber(timestamp) {
    const elapsed = timestamp - startTime;
    const progress = Math.min(elapsed / duration, 1);
    const easedProgress = easeOutQuad(progress, 0, 1, 1);
    const value = Math.floor(start + (end - start) * easedProgress);
    element.innerHTML = value;

    if (progress < 1) {
      requestAnimationFrame(updateNumber);
    }
  }

  requestAnimationFrame(updateNumber);
}

// 使用示例
animateWithEasing("counter", 0, 1000, 2000);

实现注意事项

  • 性能考虑:对于大量数字或频繁更新,使用 requestAnimationFramesetInterval 更高效
  • 平滑度控制:通过调整持续时间或帧率可以控制动画的平滑程度
  • 浏览器兼容性:现代方法如 requestAnimationFrame 在大多数现代浏览器中都支持
  • 中断处理:应考虑在动画过程中可能的中断情况,如页面切换或元素移除

以上方法可以根据具体需求选择使用,简单的计数器可以使用 setInterval,而更复杂的动画效果建议使用 requestAnimationFrame 或专门的动画库。

js 实现数字滚动

标签: 数字js
分享给朋友:

相关文章

js实现计算器

js实现计算器

实现基础计算器功能 使用JavaScript创建一个基础计算器需要处理数字输入、运算符逻辑和结果显示。以下是一个简单的实现示例: let currentInput = '0'; let previ…

js实现验证码

js实现验证码

使用Canvas生成图形验证码 在HTML中创建一个Canvas元素用于绘制验证码。通过JavaScript随机生成数字或字母组合,并添加干扰线、噪点等干扰元素增强安全性。 <canvas…

js实现继承

js实现继承

原型链继承 通过将子类的原型指向父类的实例实现继承。子类实例可以访问父类原型上的属性和方法。 function Parent() { this.name = 'parent'; } Parent…

js实现日历

js实现日历

实现日历的基本思路 使用JavaScript实现日历的核心是动态生成日期表格,并处理月份切换逻辑。需要计算当前月的天数、起始星期几,并动态渲染到页面上。 获取当前日期信息 通过Date对象获取当前年…

js实现图片放大缩小

js实现图片放大缩小

实现图片放大缩小的基础方法 使用CSS的transform: scale()属性可以快速实现图片的放大缩小效果。通过JavaScript动态修改该属性值: const img = document…

js实现类

js实现类

JavaScript 实现类的几种方式 JavaScript 是一种基于原型的语言,但在 ES6 之后引入了 class 语法糖,使得实现类更加直观。以下是几种常见的实现方式: 使用构造函数 传统的…