简体   繁体   English

Jquery Animate无法完成动画的问题

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

I have a fair bit of animation running on my page, and I think its causing this particular animation to stutter, and then not finish its animation some of the time. 我的页面上运行着相当多的动画,并且我认为这会导致特定动画停顿,然后有时无法完成其动画。

I have written this to animation some percentage bars on the page. 我已经在页面上的一些百分比条上写了动画。

I have tried adding a timeout function so that it gives the other animation some time to finish up, however the timeout doesnt work and it seems to fire off immediately after the page loads. 我尝试添加超时功能,以便它给其他动画一些时间来完成,但是超时不起作用,它似乎在页面加载后立即触发。 Any help making sure my percentages finish would be amazing 确保我完成百分比的任何帮助都将是惊人的

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);
});

Here is a HTML example of what should be animated, but failed at 91% 这是应该制作动画的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>

You are calling dopercentage() immediately, then passing the result to setTimeout. 您正在立即调用dopercentage() ,然后将结果传递给setTimeout。

setTimeout requires a function reference , not the result of a function call. setTimeout需要函数引用 ,而不是函数调用的结果。

Just pass the function reference (name): 只需传递函数引用(名称)即可:

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

Aside from that, I just had to remove the initial 91% text from the sample. 除此之外,我只需要删除样本中最初的91%文本。 When the code was not firing I wound up with your test value :) 当代码不触发时,我用您的测试值结束了:)

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

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

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