如何每秒增加价值来制作动画数字。就像在这个插件中一样
http://aishek.github.io/jquery-animateNumber

HTML

<div class="earned">171655.10457862</div>


这是jQuery代码

var DOGEbal = 171655.10457862;
var per_second = 1.56948984;
interval = setInterval(function(){
  DOGEbal += per_second;
  DOGEbalStack = DOGEbal.toString();
  DOGEbalStack = DOGEbalStack.slice(0, (DOGEbalStack.indexOf("."))+9);
  $(".earned").html(DOGEbalStack);
}, 1000);


因此,我想为添加per_second.earned的数字设置动画。像animateNumber插件一样每秒

这是https://jsfiddle.net/uzd83qw6/1/示例。如何为该示例制作动画?

最佳答案

您可以采取“每秒”的步骤,并将其转换为“每秒”的延迟:



function animate($target, fromValue, incrementPerSecond) {
  var incrementPerMillisecond = incrementPerSecond / 1000;
  var currentValue = fromValue;

  $target.text(currentValue);
  setInterval(function () {
    $target.text((currentValue + incrementPerMillisecond).toFixed(8));
    currentValue += incrementPerMillisecond;
  }, 1);
}

animate($('.earned'), 171655.10457862, 1.56948984);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="earned"></div>

关于javascript - 如何通过每秒增加值(value)来制作动画数字,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55600587/

10-11 14:47