代码之家  ›  专栏  ›  技术社区  ›  mhuber

Javascript:多次使用countup函数

  •  -2
  • mhuber  · 技术社区  · 7 年前

    谁能帮帮我吗。

    我找到了一个很好的倒计时脚本: https://codepen.io/alemarengo/pen/mOWqwy

    现在,我想为同一站点上的其他一些元素多次调用此函数。但设置不同。

     start += 0.125;
    

    例如,将加法从0.125设置为5.25。

    提前感谢您!

    1 回复  |  直到 7 年前
        1
  •  0
  •   raul.vila    7 年前

    这是一种可能的方法:实例化不同的 go 具有不同参数的功能:

    var start = {
      "counter-1": 7500000,
      "counter-2": 1500000
    };
    var speed = 1000;
    $(document).ready(function() {
      launch("counter-1", 1.5);
      launch("counter-2", 100)
    });
    
    function launch(elem, increment) {
      var go = goFactory(elem, increment)
      go();
      setInterval(go, speed);
    }
    
    function goFactory(elemId, increment) {
      function go() {
        $("#" + elemId).html(start[elemId].toFixed(0));
        start[elemId] += increment;
      }
    
      return go;
    }
    div {
      font-weight: normal;
      letter-spacing: 5px;
      padding: 6px;
      font-size: 1.8em;
      font-family: 'Gill Sans';
      width: 500px;
      text-align: center;
      height: auto;
      color: #333;
      display: block;
      margin: 0px auto;
      vertical-align: middle;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <html>
    
    <head>
      <title>Contatore</title>
    </head>
    
    <body>
      <div id="counter-1"></div>
      <div id="counter-2"></div>
    </body>
    
    </html>