javascript - 动画完成后如何运行jquery函数?

标签 javascript jquery animation jquery-animate

所以我对 javascript 和 jquery 很陌生。需要有关如何在第一个函数完成后运行第二个函数的帮助吗? 第一个功能是进度条,第二个功能是我想在进度条动画完成后淡入的表单。

脚本:

$(function() {
  var msecsPerUpdate = 1000 / 60; // # of milliseconds between updates, this gives 60fps
  var progress = $('progress');
  var duration = 1; // secs to animate for
  var interval = progress.attr('max') / (duration * 1000 / msecsPerUpdate);

  var animator = function() {
    progress.val(progress.val() + interval);
    if (progress.val() + interval < progress.attr('max')) {
      setTimeout(animator, msecsPerUpdate);
    } else {
      progress.val(progress.attr('max'));
    }
  }

  $('a#forgot').click(animator);
});

$(function(e) {
  e.preventDefault();
  $('generator#forgot').css('bottom', '5px');
  $(this).fadeOut('medium', function() {
    $('a#back').fadeIn();
  });

});

提前致谢!

最佳答案

我会向您的 animator 函数添加一个回调函数,使其看起来像这样:

var animator = function(callback) {
  progress.val(progress.val() + interval);
  if (progress.val() + interval < progress.attr('max')) {
    setTimeout(animator, msecsPerUpdate);
  } else {
    progress.val(progress.attr('max'));
    callback();
  }
}

然后你可以这样调用它:

$('a#forgot').click(function() {
  animator(function() {
    console.log('this runs after your animation has completed');
  }); 
});

注意:这是一个简化的代码片段,没有您可能想要添加的参数或错误处理。

关于javascript - 动画完成后如何运行jquery函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30570544/

相关文章:

javascript - JQuery:动态创建的 html 不响应 JQuery 函数

c++ - 如何在OpenGL中处理纹理动画?

javascript - Canvas 动画不断重绘前几帧

jquery Accordion 菜单动画过渡

javascript - 通过一次更新对有序列表中的元素重新排序

javascript - 语义 UI checkall 不起作用

javascript - 在 firefox 中切换浏览器选项卡时 jquery 动画中断

javascript - 使用动态对象名称访问对象

javascript - extJs 从嵌套对象访问父函数

javascript - 使用 ng-2 ui-router-2 处理 Angular 4 中的尾部斜杠