使用 JavaScript/jQuery 预加载图像的最佳方式是什么?
我完全意识到这个问题已经在任何地方被提出和回答,无论是在场还是场外。 然而,每次似乎都有不同的答案,例如 这个,这个和那个。
我不在乎它是否使用 jQuery - 重要的是它可以工作,并且是跨浏览器的。]
那么,预加载图像的最佳方法是什么?
I'm fully aware that this question has been asked and answered everywhere, both on SO and off. However, every time there seems to be a different answer, e.g. this, this and that.
I don't care whether it's using jQuery or not - what's important is that it works, and is cross-browser.]
So, what is the best way to preload images?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
不幸的是,这取决于您的目的。
如果您打算将图像用于风格目的,那么最好的选择是使用精灵。
http://www.alistapart.com/articles/sprites2
但是,如果您打算使用 中的图像 标签,那么您需要预先加载它们
(修改后的源代码取自 在 JavaScript 中预加载多个图像的最佳方法是什么?)
使用
new Image()
不涉及使用 DOM 方法的费用,但对指定图像的新请求将添加到队列中。 由于此时图像并未实际添加到页面,因此不涉及重新渲染。 不过,我建议将其添加到页面末尾(如果可能的话,所有脚本都应该如此),以防止它占用更多关键元素。编辑:编辑以非常正确地反映评论,指出需要单独的
Image
对象才能正常工作。 谢谢,我很抱歉没有更仔细地检查它。Edit2:进行编辑以使可重用性更加明显
编辑3(3年后):
由于浏览器处理不可见图像的方式发生变化(显示:无,或者如本答案所示,从未附加到document)首选新的预加载方法。
您可以使用 Ajax 请求强制提前检索图像。 例如,使用 jQuery:
或者在我们前面的示例的上下文中,您可以执行以下操作:
请注意,这不适用于按原样良好的精灵的情况。 这仅适用于照片库或带有图像的滑块/轮播等,其中图像未加载,因为它们最初不可见。
另请注意,此方法不适用于 IE(ajax 通常不用于检索图像数据)。
Unfortunately, that depends on your purpose.
If you plan to use the images for purposes of style, your best bet is to use sprites.
http://www.alistapart.com/articles/sprites2
However, if you plan to use the images in <img> tags, then you'll want to pre-load them with
(modified source taken from What is the best way to preload multiple images in JavaScript?)
using
new Image()
does not involve the expense of using DOM methods but a new request for the image specified will be added to the queue. As the image is, at this point, not actually added to the page, there is no re-rendering involved. I would recommend, however, adding this to the end of your page (as all of your scripts should be, when possible) to prevent it from holding up more critical elements.Edit: Edited to reflect comment quite correctly pointing out that separate
Image
objects are required to work properly. Thanks, and my bad for not checking it more closely.Edit2: edited to make the reusability more obvious
Edit 3 (3 years later):
Due to changes in how browsers handle non-visible images (display:none or, as in this answer, never appended to the document) a new approach to pre-loading is preferred.
You can use an Ajax request to force early retrieval of images. Using jQuery, for example:
Or in the context of our previous example, you could do:
Note that this doesn't apply to the case of sprites which are fine as-is. This is just for things like photo galleries or sliders/carousels with images where the images aren't loading because they are not visible initially.
Also note that this method does not work for IE (ajax is normally not used to retrieve image data).
Spriting
正如其他人所提到的,由于多种原因,Spriting 效果很好,但是,它并不像看起来那么好。
但处理不规则形状是很棘手的。 将所有新图像合并到新图像中是另一个烦恼。
使用 的低插孔方法 标签
如果您正在寻找最明确的解决方案,那么您应该采用我仍然喜欢的低杰克方法。 创建 链接到文档末尾的图像,并将
宽度
和高度
设置为1x1像素,然后将它们放入隐藏的div中。 如果它们位于页面末尾,则会在其他内容之后加载。Spriting
As others have mentioned, spriting works quite well for a variety of reasons, however, it's not as good as its made out to be.
Dealing with irregular shapes is tricky though. Combining all new images into the new one is another annoyance.
Low jack approach using <img> tags
If you are looking for the most definitive solution then you should go with the low-jack approach which I still prefer. Create <img> links to the images at the end of your document and set the
width
andheight
to 1x1 pixel and additionally put them in a hidden div. If they are at the end of the page, they will be loaded after other content.截至 2013 年 1 月,此处描述的方法均不适用于我,因此这里是在 Chrome 25 和 Firefox 18 上进行测试和使用的方法。使用 jQuery 和 此插件来解决加载事件怪癖:
用法:
请注意,如果禁用缓存,您将得到混合结果,当开发人员工具打开时,Chrome 上默认情况下是禁用缓存的,所以记住这一点。
As of January 2013 none of the methods described here worked for me, so here's what did instead, tested and working with Chrome 25 and Firefox 18. Uses jQuery and this plugin to work around the load event quirks:
Usage:
Note that you'll get mixed results if the cache is disabled, which it is by default on Chrome when the developer tools are open, so keep that in mind.
在我看来,使用一些库引入的 Multipart XMLHttpRequest 将是未来几年的首选解决方案。 然而 IE < v8,仍然不支持data:uri(即使IE8也有有限的支持,最多允许32kb)。 以下是并行图像预加载的实现 - http://code.google.com/ p/core-framework/wiki/ImagePreloading ,它捆绑在框架中,但仍然值得一看。
In my opinion, using Multipart XMLHttpRequest introduced by some libraries will be a preferred solution in the following years. However IE < v8, still don't support data:uri (even IE8 has limited support, allowing up to 32kb). Here is an implementation of parallel image preloading - http://code.google.com/p/core-framework/wiki/ImagePreloading , it's bundled in framework but still worth taking a look.
这是很久以前的事了,所以我不知道有多少人仍然对预加载图像感兴趣。
我的解决方案更加简单。
我刚刚使用了CSS。
This was from a long time ago so I dont know how many people are still interested in preloading an image.
My solution was even more simple.
I just used CSS.
这是我的简单解决方案,在加载图像后淡入图像。
Here goes my simple solution with a fade in on the image after it is loaded.
对于我的用例,我有一个带有我想要预加载的全屏图像的轮播。 然而,由于图像按顺序显示,并且每个图像可能需要几秒钟的时间来加载,因此按顺序加载它们很重要。
为此,我使用了异步库的
waterfall()
方法 (https://github.com/async#waterfall")。 com/caolan/async#waterfall)这通过创建一个函数数组来工作,每个函数都会传递参数
callback()
,它需要执行该参数才能调用下一个函数数组。 callback() 的第一个参数是错误消息,如果提供非 null 值,则会退出序列,因此我们每次都传递 null。For my use case I had a carousel with full screen images that I wanted to preload. However since the images display in order, and could take a few seconds each to load, it's important that I load them in order, sequentially.
For this I used the async library's
waterfall()
method (https://github.com/caolan/async#waterfall)This works by creating an array of functions, each function is passed the parameter
callback()
which it needs to execute in order to call the next function in the array. The first parameter ofcallback()
is an error message, which will exit the sequence if a non-null value is provided, so we pass null each time.请参阅:
http://www.mattfarina.com/2007/02/01/preloading_images_with_jquery
SO相关问题:
jquery隐藏预加载
See this:
http://www.mattfarina.com/2007/02/01/preloading_images_with_jquery
Related question on SO:
jquery hidden preload