简体   繁体   English

jQuery显示每个图像的预加载器?

[英]Jquery show preloader for every image?

I've a website with a lot of images: 我的网站上有很多图片:

<img class='thumb' src=thumbs/image1.jpg/>
<img class='thumb' src=thumbs/image2.jpg/>
<img class='thumb' src=thumbs/image3.jpg/>
<img class='thumb' src=thumbs/image4.jpg/>

I wonder if I can display a loading-div for every single image and when it's loaded the div hides? 我想知道是否可以为每个图像显示loading-div,并且在加载时div隐藏吗? I'm using jquery for most of my animations. 我的大多数动画都使用jquery。 Can i solve that with jquery. 我可以用jQuery解决吗。

It would be cool if every image shows this loading-div centered in the middle of the image. 如果每个图像都显示该loading-div居中于图像中间,那将很酷。 When the image is loaded the div should hide! 加载图像后,div应该隐藏!

Is that possible? 那可能吗? Thank you for your help! 谢谢您的帮助!

Something like this should do it: 这样的事情应该做到:

$("img.thumb").after('<img class="spinner" scr="spinner.gif"/>');
$("img.thumb").load(function() {
    $(this).next(".spinner").remove();
}).each(function() {

    // if the images is loaded from cache, trigger the load event
    if(this.complete) $(this).trigger('load');
});

But as others have commented, spinners all over the page will probably look horrible. 但是,正如其他人所评论的那样,整个页面上的微调器可能看起来很恐怖。

http://api.jquery.com/load-event/ http://api.jquery.com/load-event/

Would it not be easier to do it in CSS? 在CSS中这样做会更容易吗?

.thumb {
    background: url(spinner.gif) center center;
}

When the image has loaded it'll hide the background image. 加载图像后,它将隐藏背景图像。
Note : You may need to give it a width and height, which shouldn't be a problem if they're all thumbnails. 注意 :您可能需要给它一个宽度和高度,如果它们都是缩略图,那应该没问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM