如何在jQuery插件

时间:2015-10-30 06:35:02

标签: javascript jquery

我正在使用位于https://github.com/tombruijn/counter.js

的jQuery插件

但我想在此显示随机数。演示位于

http://yipi.in

我的代码如下:

<script type="text/javascript">
    var counterUp = $("#counter-up");
    counterUp.counter({
        autoStart: true,
        duration: 21000,
        countFrom: 10,
        countTo: 2024,
        placeholder: 0,
        easing: "easeOutCubic",
        onStart: function() {
            document.getElementById("trigger").innerHTML = "Result"
        },
        onComplete: function() {
            document.getElementById("trigger").innerHTML = "Result"
        }
    });
    var counterDown = $("#counter-down");
    counterDown.counter({
        autoStart: true,
        countTo: 0,
        duration: 7000,
        easing: "easeOutCubic"
    });

    $(document).on("click", "button", function() {
        counterUp.counter("start");
        counterDown.counter("start");
    });
</script>

1 个答案:

答案 0 :(得分:0)

您可以使用Math.random函数

var randNumber = Math.random() * 2013;

它会给出1到1000之间的返回数字;

  var start = 10;
  var end = Math.ceil(Math.random() * 2013) + 10;

  var counterUp = $("#counter-up");
  counterUp.counter({
    autoStart: true,
    duration: 21000,
    countFrom: start,
    countTo: end,
    placeholder: 0,
    easing: "easeOutCubic",
    onStart: function() {
      document.getElementById("trigger").innerHTML = "Result"
    },
    onComplete: function() {
      document.getElementById("trigger").innerHTML = "Result"
    }
  });
  var counterDown = $("#counter-down");
  counterDown.counter({
    autoStart: true,
    countTo: 0,
    duration: 7000,
    easing: "easeOutCubic"
  });

  $(document).on("click", "button", function(){
    counterUp.counter("start");
    counterDown.counter("start");
  });