创建旋转数字直到达到总数

时间:2021-08-21 21:28:12

I had some help yesterday to achieve this: http://jsfiddle.net/hopkins_matt/513ng07d/ (Thanks to Matt Hopkins) -----

昨天我得到了一些帮助:http://jsfiddle.net/hopkins_matt/513ng07d/(感谢Matt Hopkins)-----

function numberWithCommas(x) {
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

function timeSince() {
    var prevTime = new Date(2015,8,8,0,0);
    var thisTime = new Date();
    return (thisTime.getTime() - prevTime.getTime()) / 1000;
}

function parcelCount() {
    var secDiff = timeSince();

    var leftNum = document.getElementById("left");
    var midNum = document.getElementById("mid");
    var leftNumCount = Math.round(((76/60) * secDiff) + 40093794);
    var midNumCount = Math.round(((43/60) * secDiff) + 22874098);

    leftNum.innerHTML = numberWithCommas(leftNumCount);
    midNum.innerHTML = numberWithCommas(midNumCount);
}
parcelCount();
setInterval(parcelCount, 1000);

I also want to create a spinning total until the final figure is reached...

我还要创建一个旋转总数,直到达到最终数字...

I.E. We have shipped to 190 countries, is it possible to spin this number from 0-190 when the text is reached on screen?

I.E.我们已经发货到190个国家,是否可以在屏幕上显示文本时从0到190旋转此数字?

So it would spin numbers until it reached the 190, then stop.

所以它会旋转数字直到它达到190,然后停止。

Any help would be appreciated :)

任何帮助,将不胜感激 :)

1 个解决方案

#1


2  

If you want just an animation from 0 - 190 you can use this.

如果你想要一个0到190之间的动画,你可以使用它。

Just loop one function increasing your display variable.

只需循环一个函数来增加显示变量。

var totalShipped = 190;
var shippedDisplay = 0;
var shippedStep = totalShipped / (2 * 1000 / 100); // Animation duration 2 sec
function animateShipped() {
  if (shippedDisplay > totalShipped)
    shippedDisplay = totalShipped;
  document.getElementById("shipped").innerHTML = Math.round(shippedDisplay);
  if (shippedDisplay < totalShipped) {
    shippedDisplay += shippedStep;
    setTimeout(animateShipped, 100);
  }
}
animateShipped();
<h3>Shipped</h3>
<span id="shipped"></span>

#1


2  

If you want just an animation from 0 - 190 you can use this.

如果你想要一个0到190之间的动画,你可以使用它。

Just loop one function increasing your display variable.

只需循环一个函数来增加显示变量。

var totalShipped = 190;
var shippedDisplay = 0;
var shippedStep = totalShipped / (2 * 1000 / 100); // Animation duration 2 sec
function animateShipped() {
  if (shippedDisplay > totalShipped)
    shippedDisplay = totalShipped;
  document.getElementById("shipped").innerHTML = Math.round(shippedDisplay);
  if (shippedDisplay < totalShipped) {
    shippedDisplay += shippedStep;
    setTimeout(animateShipped, 100);
  }
}
animateShipped();
<h3>Shipped</h3>
<span id="shipped"></span>