jQuery fadeIn在完成每个DIV加载图像时?

时间:2011-05-04 04:08:47

标签: javascript jquery html

我有一个显示很多图片的网站。而不是显示它们并观察它的负载,有没有办法在它完成加载后淡入它?有点jQuery和javascript精彩世界的新手。

现在我从PHP文件中获取数据,该文件调用mySQL服务器并获取最新图像。它只返回一大块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");

  }
});
});

以下是包含每张图片的ONE 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>

当然,它会消失,但用户会看到它的负载。

最好的方法是什么?

谢谢,

4 个答案:

答案 0 :(得分:2)

使用style ='display:none'加载img标记并绑定图像的load()事件以淡入它。文档:http://api.jquery.com/load-event/

样品:

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

答案 1 :(得分:2)

你可以使用jquery image .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();
    });
  }
});

或者您可以使用live()绑定动态img元素

答案 2 :(得分:1)

首先,使用.load()而不是ajax,因为它更易于使用

然后,作为load()函数的回调,将fadeIn()绑定到load事件

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

答案 3 :(得分:1)

哦..使用load()

您可以在图像加载完成后执行功能(取消隐藏包含的div)。

看看这个例子 http://www.stoimen.com/blog/2009/08/05/jquery-check-whether-image-is-loaded/