0

数を増やして56,941で停止する関数があります。また、異なる幅のサイズにアニメーション化するdivコンテナもあります。

関数が停止したら、divコンテナのアニメーションを停止したいと思います。これが私のコードです:

/****COUNTER****/

jQuery.fn.extend({
      ts : function (from, to, time) {
        var steps = 1,
            self = this,
            counter;

        if (from - to > 0) {
          steps = -1;
        };

        from -= steps;

        function step() {
          self.text(addCommas(from += steps));

          if ((steps < 0 && to >= from) || (steps > 0 && from >= to)) {
            clearInterval(counter);
          };
        };

        counter = setInterval(step, time || 5);
      }
    });


    var total = $('.total').ts(56100,56941);




    /****COMMA***/

    function addCommas(nStr)
    {
      nStr += '';
      x = nStr.split('.');
      x1 = x[0];
      x2 = x.length > 1 ? '.' + x[1] : '';
      var rgx = /(\d+)(\d{3})/;
      while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
      }
      return x1 + x2;
    }


    /****BAR****/



    $('.bar').animate({width: "21%",},5000);    

これは可能ですか?ありがとうございました!

4

2 に答える 2

1

これを適切な場所に置いてください、私はあなたがインターバルループをクリアした直後に言うでしょう...

...
clearInterval(counter);
// stop the animation here...
$('.bar').stop();
...

呼び出す.stop()と、セレクターのすべてのアニメーションが停止します。

于 2012-10-30T19:22:18.010 に答える
1

stop()関数(http://api.jquery.com/stop/ )を使用して、jQueryアニメーション関数を早期に終了します。この場合は次のようになります。

$('.bar').stop();
于 2012-10-30T19:22:19.973 に答える