2012-08-03 124 views
1

我做了一個啓動畫面的jQuery插件。它用了jQuery的動畫功能,並通過以下initalized和觸發:延遲,直到jQuery動畫完成

$('#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); 

回答

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 
    } 
} 

這只是一個例子,你必須弄清楚哪些選項傳遞以及如何將其綁定到您的插件自己。

+0

完美里面的代碼之前執行。現在它可以工作。 – olif 2012-08-03 18:36:49

0

難道你不能綁定到jQuery中的.done功能嗎?

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

+0

自制插件不一定有權訪問promise,除非您將其添加到插件中。 – adeneo 2012-08-03 18:33:58

0

,因爲這些調用是異步的,你可以把你的Ajax代碼只是飛濺代碼前收盤爲防濺功能之後的代碼將閃功能