等待动画停止

时间:2016-07-15 16:30:38

标签: jquery animation

我必须等到动画结束才能获得正确的偏移()。顶部值。

我尝试了不同的方法(比如promise),但没有一种方法:console.log立即被点击。为什么?我该怎么办?

我的一次尝试:

$('#main').on('click', 'section > span', function() {

    el = $(this);

    var effect = function() {
      return $('.content').removeClass('showing').addClass('hiding').slideUp(300).attr("aria-hidden","true").closest('section').css('min-height','0');
    }

    $.when( effect() ).done(function() {
      var showTop = el.offset().top - 110;
      console.log(showTop);
      $('html, body').animate({scrollTop:showTop}, 'fast', function() {
        content.removeClass('hiding').addClass('showing').removeAttr("aria-hidden").slideDown(600);
      });
    });

}

感谢。

1 个答案:

答案 0 :(得分:2)

$.when的参数应该是一个承诺/延期。您可以使用.promise()来获取具有效果的元素的承诺。

var effect = function() {
    $('.content').removeClass('showing').addClass('hiding').slideUp(300).attr("aria-hidden","true").closest('section').css('min-height','0');
    return $('.content').promise();
}
相关问题