简体   繁体   English

JS / CSS:隐藏HTML文件时预加载图像

[英]JS/CSS: Preload images from the HTML file while they are hidden

Let's assume I have several img tags in my html document, all of them are supposed to be shown in a slideshow that shows one image at a time. 假设我的html文档中有几个img标记,所有这些标记都应该以幻灯片形式显示,一次显示一个图像。

<div id="gallery">
    <img class="slide" src="images/1.png" width="600" height="400" alt=""/>
    <img class="slide" src="images/2.png" width="600" height="400" alt=""/>
    <img class="slide" src="images/3.png" width="600" height="400" alt=""/>
    <img class="slide" src="images/4.png" width="600" height="400" alt=""/>
</div>

I don't want to display these images until they are loaded all and once they are loaded all I would like to present them as a slideshow (that part is not important). 我不希望显示这些图像,直到它们被加载全部并且一旦加载所有我想将它们呈现为幻灯片放映(该部分并不重要)。

The thing I cannot really understand how it works is the loading of images while being hidden. 我无法真正理解它是如何工作的是在隐藏时加载图像。 Let's assume I hide them all with display:none after $.ready, would this not prevent them from loading in some browsers? 让我们假设我将它们全部隐藏起来display:none在$ .ready之后display:none ,这会不会阻止它们在某些浏览器中加载吗? If I leave them visible, some images may appear before all of them are loaded, which is undesirable. 如果我让它们可见,则在加载所有图像之前可能会出现一些图像,这是不合需要的。 I don't want to load them using ajax. 我不想使用ajax加载它们。

Once again what I am trying to do: 我再一次尝试做什么:

  • have the images in html, not load them with ajax 将图像放在html中,而不是用ajax加载它们

  • do not display them until all of them are loaded 在加载所有这些之前不要显示它们

Some advice how to achieve this? 一些建议如何实现这一目标? I am not interested of how to display them when all are loaded, I am interested in how to hide them while loading and that they would still load. 我对加载所有内容时如何显示它们不感兴趣,我对如何在加载时隐藏它们以及它们仍然会加载感兴趣。 Thank you! 谢谢!

First, you need to ensure that none of the images show initially. 首先,您需要确保最初没有显示任何图像。 In order to do that, you need to alter your stylesheet. 为此,您需要更改样式表。

Note that just because the css is set to display: none , does not mean the browser doesn't load it. 请注意,只是因为CSS设置为display: none并不意味着浏览器不会加载它。 The browser still loads the image, it simply does not display it. 浏览器仍然加载图像,它根本不显示它。

What you do in a case like this, in your stylesheet: 你在样式表中在这种情况下做了什么:

/* prevent images from being displayed before they are loaded ready */
#gallery img {
    display: none;
}

Then, with some jQuery, show the images as they are fully loaded: 然后,使用一些jQuery,在完全加载时显示图像:

jQuery(function($) {
    $('#gallery img').load(
        function() {
            $(this).show();
        }
    );
});

This would show each image as it is loaded. 这将显示加载时的每个图像。

OR 要么
If you want to wait until all of them are loaded, your jQuery would be a bit different: 如果你想等到所有这些都被加载,你的jQuery会有点不同:

jQuery(function($) {
    // Get count of images
    var icount = $('#gallery img').length;
    // Initialize count to track loaded images
    var lcount = 0;
    $('#gallery img').load(
        function() {
             // If the loaded images is equal to the total images, show them all
             if (++lcount >= icount) {
                 $('#gallery img').show();
             }
        }
    );
});

OR 要么
Finally, if you would just prefer to wait until the entire page is loaded before showing them, then use this jQuery: 最后,如果你只是想在显示它们之前等到整个页面被加载,那么使用这个jQuery:

jQuery(window).load(function($) {
    $('#gallery img').show();
});

The best easy solution to be sure that images will be loaded and will not be any problem is to: 确保图像加载并且不会出现任何问题的最佳简单解决方案是:

.preload{
    position: absolute;
    top: -9999px;
    left: -9999px;
}

And add a copy of your images' html inside preload. 并在预加载中添加图像'html的副本。

load those in javascript 加载那些在JavaScript中

(function(){
    imgURLs = ['a.jpg', 'b.jpg', 'c.jpg', 'd.jpg'];
    imgs = [];
    var i = 0,
        len = imgURLs.length;
    loadAsset();

    function loadAsset(){
        if (i < len){

            imgs[i] = new Image();
            imgs[i].src = imgURLs[i];
            imgs[i].onload = function(){
                i++;
                loadAsset();
            }
        } else {
            //some init function for the slideshow
            initSlideShow();
        }
    }
})();

You could load the images using the jQuery "Deferred" object, also known as a promise. 您可以使用jQuery“Deferred”对象加载图像,也称为promise。

var promises = [];
$('#gallery .slide').each(function(){
    var promise = $.Deferred();
    $(this).load(function(){ promise.resolve();} );
    promises.push(promise);
});
$.when.apply($,promises).then(function(){
    $('#gallery .slide').show();
});

http://api.jquery.com/category/deferred-object/ http://api.jquery.com/category/deferred-object/

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

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