2011-05-04 115 views
2

我有一個網站,顯示大量的圖片。沒有顯示它們並觀察它加載,有沒有辦法在加載完成後淡入其中?有點新的jQuery和JavaScript的美妙世界。當完成加載每個單獨的DIV與圖像時jQuery淡入?

現在我從中調用MySQL服務器並獲取最新的圖像的PHP文件中獲取數據。它只是返回的HTML大塊顯示:

// If the user clicks the logo 
$("#logo").click(function() { 
$("#right_bar_wrapper").animate({ height: 'toggle', opacity: 'toggle' }, '200'); 
var thePostData = "username=c"; 
$.ajax({ 
    type: "POST", 
    url: "http://myflashpics.com/v2/process_newsfeed.php", 
    data: thePostData, 
    success: function(theRetrievedData) { 
    document.getElementById('right_bar_wrapper').innerHTML = theRetrievedData; 
    $("#right_bar_wrapper").fadeIn("200"); 

    } 
}); 
}); 

這裏是其中一個DIV看起來像一個包含每個圖像:

<div class='sidebar_image_box_newsfeed'> 
<div class='sidebar_image_box_newsfeed_user_info makeProfileAppear' id='user_coultonvento'><img src='http://myflashpics.com/get_image.php?short_string=kmdp&size=thumbnail' />TheAmazingCoultoniusTheFourneeth...</div> 
<img src='http://myflashpics.com/get_image.php?short_string=6v9o&size=big' id='image_6v9o' class='makePictureBig' style='width: 180px;' /> 
<div class='sidebar_image_box_newsfeed_user_info_comments' style='float: right; margin-top: -1px; margin-left: 20px; display: none;' id='bigpicture_comment_6v9o'>9</div> 
<div class='sidebar_image_box_newsfeed_caption'>Usama bin laden? I believe that's a typo, Fox. </div> 
<div class='sidebar_image_box_newsfeed_user_info_comments' id='littlepicture_comment_6v9o'>9</div> 
<div style='clear: both;'></div> 
</div><div class='sidebar_image_box_newsfeed'> 
<div class='sidebar_image_box_newsfeed_user_info makeProfileAppear' id='user_BrandonVento'><img src='http://myflashpics.com/get_image.php?short_string=e4r7&size=thumbnail' />Brandon Vento</div> 
<img src='http://myflashpics.com/get_image.php?short_string=o1sk&size=big' id='image_o1sk' class='makePictureBig' style='width: 180px;' /> 
<div class='sidebar_image_box_newsfeed_user_info_comments' style='float: right; margin-top: -1px; margin-left: 20px; display: none;' id='bigpicture_comment_o1sk'>9</div> 
<div class='sidebar_image_box_newsfeed_caption'></div> 
<div class='sidebar_image_box_newsfeed_user_info_comments' id='littlepicture_comment_o1sk'>9</div> 
<div style='clear: both;'></div> 
</div> 

當然,這淡入但用戶看它加載。

什麼會做的最好的方式是這樣?

謝謝,
庫爾頓

+0

看看你有什麼用看它加載意思? – Nick 2011-05-04 04:16:00

+0

@Nick:觀看圖片加載。在谷歌瀏覽器中,您會看到圖像的頂部,然後慢慢地開始查看圖像的其餘部分。把它隱藏起來,直到它被加載並讓它淡入? – iosfreak 2011-05-04 04:17:45

回答

2

可以使用jquery圖像.load()函數來做到這一點

$.ajax({ 
    type: "POST", 
    url: "http://myflashpics.com/v2/process_newsfeed.php", 
    data: thePostData, 
    success: function(theRetrievedData) { 
    document.getElementById('right_bar_wrapper').innerHTML = theRetrievedData; 
    $("#right_bar_wrapper").fadeIn("200"); 
    //here do some trick 
    $("#right_bar_wrapper img").load(function(){ 
     $(this).fadeIn(); 
    }); 
    } 
}); 

,也可以使用直播()結合動態IMG元件

+0

+1,但你忘了刪除第一個fadeIn – Andre 2011-05-04 04:34:16

+0

啊是的:)感謝評論,非常好。庫爾頓,你知道嗎?安德烈在講什麼。 – 2011-05-04 04:52:20

2

負載與風格的IMG標籤=「顯示:無」和結合的圖像的負載()事件,以消除它在文檔:http://api.jquery.com/load-event/

樣品:

$(".makePictureBig").live('load', function(){ 
    $(this).fadeIn(); 
}); 
+0

如果我有多個圖像,我該怎麼做?如果這是有道理的,我想逐個淡化每個,而不是全部。 – iosfreak 2011-05-04 04:21:29

+0

負載()每個圖像加載 – ariel 2011-05-04 04:23:44

+0

後載入的圖像時,它不是獲取調用將被單獨調用。把一個'alert();'放進去,它永遠不會出現。可能的問題是什麼?有沒有特定的圖書館? – iosfreak 2011-05-04 04:41:52

1

冷杉,使用.load()而不是AJAX,因爲它更容易使用

然後,如load()函數,箱的回調d淡入()來加載事件

$("#logo").click(function() { 
    $("#right_bar_wrapper") 
    .animate({ height: 'toggle', opacity: 'toggle' }, '200') 
    .load("http://myflashpics.com/v2/process_newsfeed.php", 
      {"username" : "c"}, 
      function() { 
       $(this).bind('load',function(){ 
        $(this).fadeIn("200"); 
       }); 
      } 
); 
}); 
+0

嗯..沒有工作。我的JS沒有任何工作..也許問題是與語法? – iosfreak 2011-05-04 04:33:42

+0

它有一個額外的},我已經修復。現在試試,如果不起作用,請提供小提琴 – Andre 2011-05-04 04:36:55

+0

再次,這是一個錯字。我忘記關閉綁定功能。很高興你已經解決了這個問題。 – Andre 2011-05-04 04:50:33

相關問題