延迟直到jQuery动画完成

时间:2012-08-03 18:24:11

标签: jquery jquery-plugins jquery-animate

我制作了一个jQuery插件,它会产生一个闪屏。它使用jQuery的animate函数,并通过以下方式进行初始化和激活:

    $('#temperature_splash').splashScreen();

    $('.tempGraphButton').click(function () {

        $('#temperature_splash').splashScreen('splash', 300);

         //Here, I would like to make an ajax request when the animation has finished
    }

我想在插件中的动画完成后制作一个ajax请求,但我不知道怎么做。有没有人有任何建议?

插件代码概述

$.fn.splashScreen = function (method) {
    // Method calling logic
    if (methods[method]) {
        return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
    } else if (typeof method === 'object' || !method) {
        return methods.init.apply(this, arguments);
    } else {
        $.error('Method ' + method + ' does not exist');
    }
}

var methods = {
    init: function () {
        //Some initializations
    },

    splash: function (time) {
        //Setting some properties

        splashScreenContent.animate({
            width: width,
            left: leftPos
        }, time).animate({
            height: height,
            top: topPos
        }, time);

        return false;
    }
}

})(jQuery);

3 个答案:

答案 0 :(得分:1)

只需添加回调

var methods = {
    init: function () {
        methods.splash(1000, function() {
            //do something when animation is finished
            methods.ajax();
        });
    },
    splash: function (time, callback) {
        splashScreenContent.animate({
            width: width,
            left: leftPos
        }, time).animate({
            height: height,
            top: topPos
        }, time, function() {
            if (typeof callback==='function') callback.call();
        });

        return false;
    },
    ajax: function() {
        //do ajax stuff
    }
}

这只是一个例子,你必须弄清楚要传递的选项以及如何将它自己绑定到你的插件中。

答案 1 :(得分:0)

你不能也加入jQuery中的.done功能吗?

methods.done(function(){ do your code here for the ajax })'

答案 2 :(得分:0)

因为这些调用是异步的,所以你可以在关闭splash代码之前将你的ajax代码放在代码之后,在splash函数内部执行splash函数之前执行代码

相关问题