使用 JQuery 预加载图像 - 从文件夹加载图像?
目前,我正在使用一个非常基本的功能来预加载图像:
function preload(arrayOfImages) {
$(arrayOfImages).each(function(){
$('<img/>')[0].src = this;
// Alternatively you could use:
// (new Image()).src = this;
});
}
// Usage:
preload([
'img/imageName.jpg',
'img/anotherOne.jpg',
'img/blahblahblah.jpg'
]);
我需要预加载相当多的小图像(组合后所有小图像都远低于 1MB),并且想知道是否有一种方法可以做到这一点,而无需单独声明每个图像。
任何建议将不胜感激!
Currently I am using a pretty basic function to pre-load images:
function preload(arrayOfImages) {
$(arrayOfImages).each(function(){
$('<img/>')[0].src = this;
// Alternatively you could use:
// (new Image()).src = this;
});
}
// Usage:
preload([
'img/imageName.jpg',
'img/anotherOne.jpg',
'img/blahblahblah.jpg'
]);
I have a fairly large number of small images I need to pre-load (all far under 1MB when combined), and was wondering if there was a way to do so without declaring each image individually.
Any advice would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用像 supplyJS 这样的加载器脚本。当加载大量“小”图像时,这非常有用,因为它的速度要快得多。
演示:http://www.typeofnan.com/lab/mxhr -stream/
加载如下所示:
此示例将直接将新加载的图像附加到
document.body
中。当然,您可以对 mime-type-listener 中的那些进行任何操作。诚然,这也需要明确指定每个图像,但它应该要快得多。
..顺便说一句,也许 supplyJS 的作者会添加一个 *。例如,.jpg,因为这实际上是个好主意;)
You could use a loader-script like supplyJS. This is pretty useful, when loading lots of "smallish" images, because it's much faster.
Demo: http://www.typeofnan.com/lab/mxhr-stream/
The loading would look like:
This example would directly append the newly loaded images to the
document.body
. Of course you could do anything with those in the mime-type-listener.Admittedly, this also requires to specify each image explicitly but it should be a whole lot faster.
..and by the way, perhaps the author of supplyJS will add a *.jpg for instance, because that's actually a great idea ;)
这并不是对你的问题的直接回答,但我的一个建议是考虑使用 CSS 精灵,即将它们全部放在一张图像中并使用 CSS 控制显示的内容。
Not really a direct answer to your question, but a suggestion I have is to consider using CSS sprites, that is put them all in one image and control what is displayed using CSS.