繁体   English   中英

Jquery Animate无法完成动画的问题

[英]Trouble with Jquery Animate not finishing it's animation

我的页面上运行着相当多的动画,并且我认为这会导致特定动画停顿,然后有时无法完成其动画。

我已经在页面上的一些百分比条上写了动画。

我尝试添加超时功能,以便它给其他动画一些时间来完成,但是超时不起作用,它似乎在页面加载后立即触发。 确保我完成百分比的任何帮助都将是惊人的

function dopercentage()
{
  $('.bar-percentage[data-percentage]').each(function () {
  var progress = $(this);
  var percentage = Math.ceil($(this).attr('data-percentage'));
  $({countNum: 0}).animate({countNum: percentage}, {
    duration: 1000,
    easing:'swing',
    step: function() {
      // What todo on every count
    var pct = '';
    if(percentage == 0){
      pct = Math.floor(this.countNum) + '%';
    }else{
      pct = Math.floor(this.countNum+1) + '%';
    }
      progress.text(pct) && progress.siblings().children().css('width',pct);
    }
  });
});
}

$(document).ready(function() {
  setTimeout( dopercentage(), 1000);
});

这是应该制作动画的HTML示例,但失败率为91%

<div id="bar-5" class="bar-main-container" style="margin-left:auto;margin-right:auto;background:#cb0050;max-width: 250px;">
        <div class="wrap">
          <div id="bar-percentage13" class="bar-percentage" data-percentage="100">91%</div>
          <div class="bar-container">
            <div class="bar" style="width: 91%;"></div>
          </div>
        </div>
      </div>

您正在立即调用dopercentage() ,然后将结果传递给setTimeout。

setTimeout需要函数引用 ,而不是函数调用的结果。

只需传递函数引用(名称)即可:

$(document).ready(function() {
  setTimeout( dopercentage, 1000);
});

除此之外,我只需要删除样本中最初的91%文本。 当代码不触发时,我用您的测试值结束了:)

JSFiddle: https ://jsfiddle.net/Ly3vj12d/1/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM