当前位置:首页 > JavaScript

js 实现数字滚动

2026-03-01 05:38:57JavaScript

js 实现数字滚动

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
分享给朋友:

相关文章

jquery.js

jquery.js

jQuery 简介 jQuery 是一个快速、简洁的 JavaScript 库,用于简化 HTML 文档遍历、事件处理、动画设计和 Ajax 交互。它的设计宗旨是“Write Less, Do Mor…

js如何实现继承

js如何实现继承

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

js实现乘

js实现乘

JavaScript 实现乘法运算 在 JavaScript 中实现乘法运算可以通过多种方式完成,包括基本运算符、函数封装以及高级算法(如大数乘法)。以下是几种常见方法: 使用基本乘法运算符 Jav…

js分组实现

js分组实现

分组实现方法 在JavaScript中,可以通过多种方式实现数组或对象的分组操作。以下是几种常见的方法: 使用Array.prototype.reduce() 通过reduce方法可以轻松实现数组分…

js树实现

js树实现

树的基本概念 树是一种非线性的数据结构,由节点和边组成。每个节点包含一个值和指向子节点的引用。树的顶部节点称为根节点,没有子节点的节点称为叶节点。 树的实现方式 在JavaScript中,树可以通过…

js分页实现

js分页实现

分页的基本原理 分页的核心是通过计算当前页码和数据偏移量,从服务器或本地数据中截取对应范围的数据进行展示。通常需要以下参数:当前页码(currentPage)、每页条数(pageSize)、总数据量(…