2011-06-23 51 views
3

我有一個小插件,用於根據幾個變量因子設置元素的高度。在頁面加載時,我想要延遲以防止高度被設置爲幾秒鐘。我知道延遲只適用於效果,有沒有辦法將我的插件註冊爲效果,還是需要以另一種方式進行操作?延遲()使用插件

慾望啓動代碼:

$(".app_advert").delay(10000).set_height(2000); 

插件代碼:

 //SetHeight plugin 
    (function($){ 
     $.fn.extend({ 
      set_height: function(time, callback){ 
        scroll = $(window).scrollTop(); 

        win_height = $(window).height(); 
        document_height = $(document).height(); 


        footer_height = $('#footer').height(); 
        footer_offset = (document_height-(win_height+scroll)); 
        footer_remainder = footer_height-footer_offset; 

       return this.each(function(){ 
        var x = $(this); 

        if(footer_height-footer_offset<=0){ 
         height = 0; 
        } else { 
         height = footer_height-footer_offset; 
        } 
        $(x).stop(true, false).animate({bottom: height}, time, function(){ 
         if (callback && typeof callback == 'function') { // make sure the callback is a function 
           callback.call(this); // brings the scope to the callback 
          } 

        }); 
       }); 
      } 
     }); 

})(jQuery); 

回答

2

您將需要手動調用jQuerys .queue()。可能看起來像;

$(".app_advert").delay(10000).queue(function() { 
    $(this).set_height(2000); 
    $(this).dequeue(); 
}); 

您甚至可以在該構造的後面鏈接更多排隊的方法。請注意,所有fx methods默認情況下都會排隊等待您想要應用到當前包裝集的任何其他方法,請調用另一個.queue()

有一個讀:http://api.jquery.com/queue

+0

我可以添加到我的插件代碼?如果是這樣,任何指針如何 –

+0

@MildFuzz:當然可以。就這樣放在那裏。 – jAndy

+0

我只是在插件的動畫周圍封裝了隊列方法,並且延遲(外部觸發)看起來很好。謝謝!! –

0

怎麼樣使用setTimeout的插件內。或者甚至更好使用虛擬方法使用setTimeout編寫自己的延遲插件,以便鏈接事件。