2010-06-24 63 views
0

在我的查詢中懶惰的道歉,會試圖更具體。只有在所有元素都加載後纔會淡入覆蓋div?

OK-如何修改下面的代碼,以便我可以保證fadeIn只會在所有其他操作完成並且html中的所有圖像都被加載後才啓動?我是否需要通過js加載圖像並在每個圖像上進行回調? $(window).load()中的一些代碼。而不是文件準備好?

目前,fadeIn被異步調用(我猜測) - 有時它會在圖像仍在加載時褪色.ovwrapper。以下內容位於由ajax腳本加載的html頭文件中。

//Reset overlay elements 
    $("#ov-image1"+bellcat).show(); 
    $("#ov-image2"+bellcat).hide(); 
    $("#ov-image3"+bellcat).hide(); 
    $("#ov-video"+bellcat).hide(); 
    $("#ovtext"+bellcat).hide(); 
    $("a#clicktxt").removeClass("highlight"); 
    $("a#clickimg1").addClass("highlight"); 
    $("a#clickimg2").removeClass("highlight"); 
    $("a#clickimg3").removeClass("highlight"); 
    $("a#clickvid").removeClass("highlight"); 
    //Fade in overlay inner wrapper 
    $(".ovwrapper").fadeIn("slow"); 
    //Resize 1st image in relation to height of image-wrapper 
    $(function(){ 
    var wh = $(window).height(); 
    var hh = $("#ovheader-wrapper"+bellcat).height(); 
    var nh = $("#ovfooter-wrapper"+bellcat).height(); 
    var ch = wh - (hh + nh); 
    $("#ovslideshow"+bellcat).css("height", ch+"px"); 
    }); 

提出的解決方案:

我的解決辦法是使用onImagesLoad jQuery插件。從這個回調隱藏加載gif,然後淡入.ovwrapper類。在(「知名度」,「看得見」)隱藏()位是因爲我需要的可視性。

$(function(){ 
      //attach onImagesLoad to the entire body 
      $('.ovslideshow').onImagesLoad({ 
       selectorCallback: selectorImagesLoaded 
      }); 
      //the selectorCallback function, invoked once when all images contained within $('body') have loaded 
      function selectorImagesLoaded($selector){ 
       //note: this == $selector. $selector will be $("body") in this example 
       $("#loading").hide(); 
       $('.ovwrapper').css('visibility','visible').hide().fadeIn('slow'); 
      } 
    }); 

完成效果是在這裏:隱藏,因爲我與圖像的位置和規模前完成覆蓋褪色搞亂: http://www.andrewstonyer.co.uk/test/index.html

+1

那麼問題是什麼? – 2010-06-24 16:56:21

+0

我想他希望有人重寫他的代碼。 – Mervyn 2010-06-24 17:25:55

回答

1

所有的jQuery的動畫函數獲得回調設施,所以你可以鏈接事件。請參閱此 http://api.jquery.com/show。你可以寫這樣的$(「#ov-image1」+ bellcat).show('slow',function(){$(「。ovwrapper」)。fadeIn(「slow」);});

0

在所有圖像上設置加載事件。讓回調函數增加一個計數變量,如果計數足夠高,則開始淡入淡出。