简体   繁体   English

我的jQuery Image预加载器功能正常工作吗?

[英]Does my jQuery Image preloader function work?

I've written a function which will load all large images from a lightbox gallery which are referenced by a link ( for those who don't know) after all small images are loaded $(window).load(). 我编写了一个函数,该函数将在所有小图像都加载$(window).load()之后,从灯箱画廊中加载所有大型图像,这些链接将通过链接(对于那些不知道的人)进行引用。

Here's the code: 这是代码:

$(window).load(function() {
    if ($('ul#gallery').length > 0) {
        // make a container, fill in images and attach it to the page body
        var c = $("<div />").attr({style: 'display: none', id: 'gallerypreload'});
        $("ul#gallery li > a").each(function() {
            $('<img />').attr('src', $(this).attr('href')).appendTo(c);
        });
        c.appendTo('body');
    }
});

The gallery looks like this: 画廊看起来像这样:

<ul id="gallery">
    <li><a href="/img/image.jpg" title="Image title"><img src="img/thumbnail.jpg" alt="Image title" /></a></li>
    <li><a href="/img/image.jpg" title="Image title"><img src="img/thumbnail.jpg" alt="Image title" /></a></li>
    <li><a href="/img/image.jpg" title="Image title"><img src="img/thumbnail.jpg" alt="Image title" /></a></li>
    <li><a href="/img/image.jpg" title="Image title"><img src="img/thumbnail.jpg" alt="Image title" /></a></li>
    <li><a href="/img/image.jpg" title="Image title"><img src="img/thumbnail.jpg" alt="Image title" /></a></li>
</ul>

I want to do this, because the browser is supposed to load all thumbnails first and THEN preload larger images for viewing. 我想这样做,因为浏览器应该首先加载所有缩略图,然后预加载较大的图像以供查看。 Also, I've written some sort of fading effect which is triggered by $(window).load(), too. 另外,我也写了某种由$(window).load()触发的衰落效果。

My Question here is: Do ALL browsers preload images attached to the body in a container? 我的问题是:是否所有浏览器都会将附着在人体上的图像预先加载到容器中? Hence it is not displayed, theoretically they shouldn't load the images, or do they? 因此,它不会显示,从理论上讲,他们不应该加载图像,还是应该加载? Should I style visibility: hidden; width:0; height:0 我是否应该设置visibility: hidden; width:0; height:0样式visibility: hidden; width:0; height:0 visibility: hidden; width:0; height:0 visibility: hidden; width:0; height:0 or is my approach working? visibility: hidden; width:0; height:0还是我的方法有效?

Thanks. 谢谢。

You can force the preload by creating javascript image objects with the src you want to preload 您可以通过使用要预加载的src创建javascript图像对象来强制进行预加载

$("ul#gallery li > a").each(function() {
    var preload = new Image();
    preload.src = $(this).attr('href');
    $('<img />').attr('src', preload.src).appendTo(c);
});

Then it shouldn't matter if the IMG tags are visible or not 然后,IMG标签是否可见无关紧要


Based on the comments below, this should be enough to preload the images: 根据以下评论,这足以预加载图像:

$("ul#gallery li > a").each(function() {
    var preload = new Image();
    preload.src = $(this).attr('href');
});

or even 甚至

$("ul#gallery li > a").each(function() {
   (new Image()).src = $(this).attr('href');
});

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

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