jQuery在加载每个单独的DIV与图像时的淡出

jQuery fadeIn when done loading each individual DIV with image?

本文关键字:DIV 淡出 图像 单独 加载 jQuery      更新时间:2023-09-26

我有一个网站,显示大量的图片。而不是显示他们,看着它加载,有没有一种方法,让它在加载完成后淡出?对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");
  }
});
});

下面是包含每个图像的一个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>

当然,它淡入,但用户看到它加载。

最好的方法是什么?

谢谢
Coulton

你可以使用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元素

加载style='display:none'的img标签,并绑定图像的Load()事件使其淡出。文档:http://api.jquery.com/load-event/

示例:

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

哦…使用load()

你可以在图片加载完成后执行一个函数(取消包含div的隐藏)。

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

首先,使用.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");
               });
           }
  );
});