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

时间:2015-06-01 09:59:11

标签: 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();
  });

});
&#13;
&#13;
&#13;

提前致谢!

1 个答案:

答案 0 :(得分:0)

我要为您的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');
  }); 
});

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

相关问题