gpt4 book ai didi

javascript - 使用 jQuery 预加载图像

转载 作者:IT老高 更新时间:2023-10-28 11:13:05 25 4
gpt4 key购买 nike

我正在寻找一种使用 JavaScript 预加载图像的快速简便的方法。如果这很重要,我正在使用 jQuery。

我在这里看到了这个(http://nettuts.com...):

function complexLoad(config, fileNames) {
for (var x = 0; x < fileNames.length; x++) {
$("<img>").attr({
id: fileNames[x],
src: config.imgDir + fileNames[x] + config.imgFormat,
title: "The " + fileNames[x] + " nebula"
}).appendTo("#" + config.imgContainer).css({ display: "none" });
}
};

但是,我想要的东西看起来有点过头了!

我知道有 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'
]);

或者,如果你想要一个 jQuery 插件:

$.fn.preload = function() {
this.each(function(){
$('<img/>')[0].src = this;
});
}

// Usage:

$(['img1.jpg','img2.jpg','img3.jpg']).preload();

关于javascript - 使用 jQuery 预加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/476679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com