繁体   English   中英

所有子动画完成时进行回调

[英]Callback when all sub animations complete

我有一个jQuery动画事件,此动画的回调事件中还有其他动画。

我需要能够检测到何时一切都在完成,然后在那个时候调用一个函数。

由于发生什么动画会有一些不同的结果,因此我正在努力使其按需工作,而上述解决方案将彻底清除它。

jQuery('.A_Item').stop().animate(Properties, 250, function () {

        var el = jQuery(sID);
        if (!IsOpen) {

            jQuery(el).stop(true, true).animateAuto("height", 250,
                function () {

                    jQuery('body').stop().animate({ scrollTop: oButton.offset().top }, 250, function () {

                        if (IsSubItem) {

                            jQuery(el).parent().stop().animateAuto("height", 500,
                                function () {
                                    var sButtonID = sID.replace('.A_Item', '').replace('#', '');
                                    jQuery('body').stop().animate({ scrollTop: jQuery('.Button[item="' + sButtonID + '"]').offset().top }, 500);
                                });

                        }

                    });
                }
            );

        }


    });

您需要将回调调用代码放在多个位置以处理这些情况。

假设引用callback是指要调用的回调方法

function callback() {
    //callback logic goes here
}

jQuery('.A_Item').stop().animate(Properties, 250, function () {
    var el = jQuery(sID);
    if (!IsOpen) {

        jQuery(el).stop(true, true).animateAuto("height", 250, function () {

            jQuery('body').stop().animate({
                scrollTop: oButton.offset().top
            }, 250, function () {

                if (IsSubItem) {

                    jQuery(el).parent().stop().animateAuto("height", 500, function () {
                        var sButtonID = sID.replace('.A_Item', '').replace('#', '');
                        jQuery('body').stop().animate({
                            scrollTop: jQuery('.Button[item="' + sButtonID + '"]').offset().top
                        }, 500, callback); //if all animations are enabled then call the callback
                    });

                } else {
                    //if the IsSubItem is not set call
                    callback();
                }

            });
        });

    } else {
        //if the open flag is set call the callback
        callback();
    }
});

如果每个if条件返回true可以在以下位置使用最后嵌套的.animate() complete回调

jQuery('body').stop()
.animate(
  { scrollTop: jQuery('.Button[item="' + sButtonID + '"]').offset().top }
  , 500, function() {
  // do stuff
});

暂无
暂无

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

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