我在下面写的函数是否足以在大多数(如果不是全部的话)当前常用的浏览器中预加载图像?
function preloadImage(url)
{
var img=new Image();
img.src=url;
}
我有一个图像URL数组,我循环遍历并为每个URL调用preloadImage函数。
我在下面写的函数是否足以在大多数(如果不是全部的话)当前常用的浏览器中预加载图像?
function preloadImage(url)
{
var img=new Image();
img.src=url;
}
我有一个图像URL数组,我循环遍历并为每个URL调用preloadImage函数。
当前回答
浏览器最好使用头部的链接标签。
export function preloadImages (imageSources: string[]): void {
imageSources
.forEach(i => {
const linkEl = document.createElement('link');
linkEl.setAttribute('rel', 'preload');
linkEl.setAttribute('href', i);
linkEl.setAttribute('as', 'image');
document.head.appendChild(linkEl);
});
}
其他回答
这种方法稍微复杂一些。在这里,你将所有预加载的图像存储在一个容器中,可能是一个div。然后你可以显示图像或在DOM中移动到正确的位置。
function preloadImg(containerId, imgUrl, imageId) {
var i = document.createElement('img'); // or new Image()
i.id = imageId;
i.onload = function() {
var container = document.getElementById(containerId);
container.appendChild(this);
};
i.src = imgUrl;
}
在这里试试,我也添加了一些评论
CSS2备选:http://www.thecssninja.com/css/even-better-image-preloading-with-css2
body:after {
content: url(img01.jpg) url(img02.jpg) url(img03.jpg);
display: none;
}
CSS3可选:https://perishablepress.com/preload-images-css3/ (H/T岭坝)
.preload-images {
display: none;
width: 0;
height: 0;
background: url(img01.jpg),
url(img02.jpg),
url(img03.jpg);
}
注意:带有display:none的容器中的图像可能无法预加载。 也许可视性:隐藏会更好,但我还没有测试过。感谢Marco Del Valle指出这一点
ECMAScript 2017兼容浏览器的解决方案
注意:如果你使用的是像Babel这样的编译器,这也可以工作。
'use strict';
function imageLoaded(src, alt = '') {
return new Promise(function(resolve) {
const image = document.createElement('img');
image.setAttribute('alt', alt);
image.setAttribute('src', src);
image.addEventListener('load', function() {
resolve(image);
});
});
}
async function runExample() {
console.log("Fetching my cat's image...");
const myCat = await imageLoaded('https://placekitten.com/500');
console.log("My cat's image is ready! Now is the time to load my dog's image...");
const myDog = await imageLoaded('https://placedog.net/500');
console.log('Whoa! This is now the time to enable my galery.');
document.body.appendChild(myCat);
document.body.appendChild(myDog);
}
runExample();
您还可以等待所有图像加载。
async function runExample() {
const [myCat, myDog] = [
await imageLoaded('https://placekitten.com/500'),
await imageLoaded('https://placedog.net/500')
];
document.body.appendChild(myCat);
document.body.appendChild(myDog);
}
或者使用Promise。所有这些都是并行加载的。
async function runExample() {
const [myCat, myDog] = await Promise.all([
imageLoaded('https://placekitten.com/500'),
imageLoaded('https://placedog.net/500')
]);
document.body.appendChild(myCat);
document.body.appendChild(myDog);
}
更多关于承诺。
更多关于“异步”函数。
更多关于解构赋值的内容。
更多关于ECMAScript 2015。
更多关于ECMAScript 2017。
我可以确认,在这个问题的方法是足以触发图像下载和缓存(除非你已经禁止浏览器这样做通过你的响应头),至少:
铬74 Safari 12 火狐66 边缘17
为了测试这一点,我做了一个小的网络应用程序,它有几个端点,每个端点都休眠10秒钟,然后提供一张小猫的照片。然后我添加了两个网页,其中一个包含<script>标记,其中每个小猫都是使用来自问题的preloadImage函数预加载的,另一个包含页面上使用<img>标记的所有小猫。
在上面所有的浏览器中,我发现如果我先访问预加载器页面,等待一段时间,然后转到带有<img>标记的页面,我的小猫会立即呈现。这表明预加载器在所有测试的浏览器中成功地将小猫加载到缓存中。
您可以在https://github.com/ExplodingCabbage/preloadImage-test上查看或试用我用于测试的应用程序。
特别要注意的是,即使循环图像的数量超过浏览器一次愿意发出的并行请求的数量,这种技术也可以在上述浏览器中工作,这与Robin的回答所建议的相反。图像预加载的速率当然会受到浏览器愿意发送多少并行请求的限制,但它最终会请求您调用preloadImage()的每个图像URL。
是的,这是可行的,但是浏览器会限制(4-8之间)实际的调用,因此不会缓存/预加载所有想要的图像。
更好的方法是在使用图像之前调用onload,如下所示:
function (imageUrls, index) {
var img = new Image();
img.onload = function () {
console.log('isCached: ' + isCached(imageUrls[index]));
*DoSomething..*
img.src = imageUrls[index]
}
function isCached(imgUrl) {
var img = new Image();
img.src = imgUrl;
return img.complete || (img .width + img .height) > 0;
}