如何使js函数显示随机数而不是计数

时间:2017-02-05 15:29:20

标签: javascript

如何更改以下功能,以便显示随机数而不是计算最终数?

$('.counter').each(function() {
  var $this = $(this),
    countTo = $this.attr('data-count');
  $({
    countNum: $this.text()
  }).animate({
    countNum: countTo
  }, {
    duration: 1000,
    easing: 'linear',
    step: function() {
      $this.text(Math.floor(this.countNum));
    },
    complete: function() {
      $this.text(this.countNum);
      var element = document.getElementById('result');
      element.style.opacity = "1";
      element.style.filter = 'alpha(opacity=1)'; // IE fallback

      var button = document.getElementById('return');
      button.style.opacity = "1";
      button.style.filter = 'alpha(opacity=1)'; // IE fallback

    }
  });
});

提前致谢。

1 个答案:

答案 0 :(得分:0)

此:

step: function() {
    $this.text(Math.floor(this.countNum));
},

应该是:

step: function() {
    var min = 5; // change min if you want to
    var max = 200; // change max if you want to
    $this.text(Math.floor(Math.random() * (max - min) + min)); // use Math.random to generate the random numbers
},
相关问题