简体   繁体   English

HTML / jQuery预加载图像

[英]HTML/jQuery Preloading images

I'm upgrading a basic photo gallery to use jQuery (woohoo!). 我正在升级基本照片库以使用jQuery(哇哦!)。

I understand the semantics of jQuery thus far, with one exception: I can't seem to wrap my brain around this whole preloading of images thing. 到目前为止,我理解jQuery的语义,但有一个例外:我似乎无法围绕整个图像预加载事物。 I already have a layout complete with divs classes and ids. 我已经有一个完整的divs类和id的布局。

Here's roughly what the image part looks like: 这大致是图像部分的样子:

<div id="main">
<img id="spotHolder" src="images/somePic.jpg">
</div>
<div id="others">
<img class="otherPics" src="images/someOtherPic.jpg">
</div>

I was using a jQuery Howto as a guide for preloading images; 我使用jQuery Howto作为预加载图像的指南; but I don't know what I would need to do (if anything?) to the html. 但我不知道我需要做什么(如果有的话?)到html。

Looking for an idea of what to do. 寻找有关做什么的想法。

If you look on this very page, a little farther over to the right (in the "Related" section), you'll see an exact duplicate of your question that just might answer it for you: 如果你看一下这个页面,向右走一点(在“相关”部分中),你会看到你的问题的完全重复,它可能会为你回答:

Preloading images with jQuery 使用jQuery预加载图像

I don't know why you would need to do anything to the HTML... To preload all images possible at once you could use: 我不知道为什么你需要对HTML做任何事情......要预先加载所有可能的图像你可以使用:

$("img").attr('src', function (_, src) {
    $("<img>").attr('src', src);
});

It would probably be a bit heavy-handed to do this, so you could use a more specific selector: 这样做可能有点笨拙,所以你可以使用更具体的选择器:

$("#gallery-container img").attr('src' /* etc. */

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

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