如何通过每秒增加价值来制作动画数字

时间:2019-04-09 20:10:24

标签: javascript jquery jquery-animate

如何通过每秒增加价值来制作动画数字。就像在这个插件中一样 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/的示例。如何为该示例制作动画?

1 个答案:

答案 0 :(得分: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>