【问题标题】:.done() callback when using custom jQuery Animations使用自定义 jQuery 动画时的 .done() 回调
【发布时间】:2017-08-18 12:33:19
【问题描述】:

我正在使用以下函数来为幻灯片制作动画,就像 slideUp() 或 slideDown():

(function($) {    
    jQuery.fn.slideLeftHide = function(speed, callback) {
      this.animate({
        width: "hide",
        paddingLeft: "hide",
        paddingRight: "hide",
        marginLeft: "hide",
        marginRight: "hide"
      }, speed, callback);
    }

    jQuery.fn.slideLeftShow = function(speed, callback) {
      this.animate({
        width: "show",
        paddingLeft: "show",
        paddingRight: "show",
        marginLeft: "show",
        marginRight: "show"
      }, speed, callback);
    }
})(jQuery);

问题是,我想在动画结束后只调用一次函数。 我尝试这样做:

$(".item").slideLeftHide(function() {
    $(this).remove();

}).done(function() {
    $(".tab").each(function() {
        $(this).attr('data-status', 'success');
    });
});

当我执行此代码时,我收到一条错误消息,指出 Cannot read property 'done' of undefined。 .then() 而不是 .done() 也会发生同样的情况。

我该如何解决这个问题?

谢谢!

【问题讨论】:

  • 你没有返回承诺,所以不能使用 done/then,使用你传递的回调,即$(".item").slideLeftHide(function() { $(this).remove(); $(".tab").each(function() { $(this).attr('data-status', 'success'); }); })

标签: javascript jquery jquery-animate deferred custom-function


【解决方案1】:
(function ($) {
    function slideLeftBinder(action) {
        return function () {
            var i = 0,
                speed = ((Number(arguments[i]) === parseFloat(arguments[i]) && !isNaN(arguments[i])) || undefined) && arguments[i++],
                callback = (typeof arguments[i] === 'function' || undefined) && arguments[i++],
                self = this;

            return new Promise(function (resolve) {
                var completed = 0;

                self.animate({
                    width: action,
                    paddingLeft: action,
                    paddingRight: action,
                    marginLeft: action,
                    marginRight: action
                }, {
                    duration: speed,
                    complete: callback,
                    always: function() {
                        if (self.length === ++completed) {
                            resolve(self);
                        }
                    }
                });
            });
        };
    }

    jQuery.fn.slideLeftHide = slideLeftBinder('hide');
    jQuery.fn.slideLeftShow = slideLeftBinder('show');
})(jQuery);

像这样使用它:

$(".item")
    .slideLeftHide(function() {
        $(this).remove();
    })
    .then(function(selector) {
        console.log('selector: ', selector);
        $(".tab").attr('data-status', 'success');
    });

【讨论】:

  • @ItayGanor 当然不是函数! .then 不是来自 $().animate 的方法。正如您所提到的,您想要 .done() 方法!这是您可以使用的list of the methods and properties
  • 当使用done()时,错误是一样的:$(...).slideLeftHide(...).done is not a function。
  • @ItayGanor 对不起,我很困惑,正如你所说,.done 不是一种方法,它是一个可以作为参数在$().animate 方法中传递的选项。我会重新制定我的答案!
  • @ItayGanor 已更新。对于我最初造成的混乱,我很抱歉,我有一段时间没有使用 jQuery!
猜你喜欢
  • 2018-05-26
  • 1970-01-01
  • 1970-01-01
  • 2016-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多