在图像加载时显示加载gif

时间:2013-01-05 16:28:35

标签: jquery loader

我有以下列表,当用户点击li时,我希望ul.list隐藏然后显示div.single并同时将img.data-img src设置为data-img li,但是在加载时我想将img src设置为li元素内的loader.gif。

<ul class="list">
<li data-id="7" data-thumb="http://test.com/assets/pdc/thumbs/66.png" data-img="http://test.com/assets/pdc/img/66.png" data-country="Australia" data-company="Big Farm" data-app="Tea" data-brand="Big Farm" data-package="500" class="product air-500 cat7">
    <img class="tip" data-country="Australia" data-brand="Big Farm" src="assets/img/ajax-loader.gif">
</li>
</ul>

<div class="single">
<img class="data-img" src="">
</div>

var preloader = $(this).find('.tip').attr('src');

$('.single aside img').attr('src', preloader);
$('.single aside img').attr('src', url).load(function () { 
            });

1 个答案:

答案 0 :(得分:1)

很难判断图像何时加载;您可能只想预加载图像(使用$("<img>").attr('src', src)就足够了。)

你可以依靠.load,但根据我的经验,这不是100%:

$("li").on('click', function () {
   var $this = $(this);
   //Show the throbber
   $this.find('.tip').show();

   //After image finishes loading, hide the `ul.list`
   $(".single .data-img").attr('src', $this.data-img).load(function () {
      $this.closest('.list').hide();    
   });
});
相关问题