简体   繁体   English

如何使用javascript或jQuery预加载具有多个srcset的图像?

[英]How to preload images with multiple srcset with javascript or jQuery?

I'm trying to build a script to preload images with jQuery, for a small application I'm working on. 我正在尝试为我正在开发的一个小型应用程序构建一个脚本,以使用jQuery预加载图像。

I've read different tutorials and right now I managed to have it working like this: 我阅读了不同的教程,现在我设法使其工作如下:

var imageList = ['img1.png', 'img2.png', 'img3.png'];
$.each(imageList, function (index, imageName) {
    var $img = $('<img>')[0];
    $img.onload = function () {
        console.log('loaded');
    };
    $img.src = imgPath + imageName;
}

This works fine. 这很好。 I load the images from an array I prepare, I then create all the img tags and then append them in the DOM where needed. 我从准备好的数组中加载图像,然后创建所有img标签,然后将它们附加到DOM中。

I'm wondering now, though, how can I do something similar if I have images with multiple srcset . 我现在想知道,如果我的图像具有多个srcset类似的srcset

Let's say I have 3 sizes for each image, but they could be more, normally I would put something like this in the html: 假设每个图片有3种尺寸,但它们可以更大,通常我会在html中输入以下内容:

<img srcset="large.jpg 1024w,
      medium.jpg 640w,
      small.jpg 320w"
   sizes="(min-width: 36em) 33.3vw, 100vw"
   src="small.jpg">

Now, how shall I apply the preloading to this? 现在,我应该如何对此应用预加载?

1) I could preload all the sizes for each image in Javascript, but this would be pointless, because the whole purpose of having multiple srcset is to load just one 2) I could put the img tag in the DOM, let the browser choose the only size needed and load from Javascript. 1)我可以预加载Javascript中每个图像的所有大小,但这将毫无意义,因为拥有多个srcset的整个目的是仅加载一个2)我可以将img标签放入DOM中,让浏览器选择仅需要大小并从Javascript加载。

The problem with the second option is that the browser is loading the images from the DOM, so why loading them again in Javascript? 第二个选项的问题是浏览器正在从DOM加载图像,那么为什么还要用Javascript再次加载它们呢? It's possible that I am completely wrong about this and maybe I'm missing something. 我可能对此完全错了,也许我错过了一些东西。 What's the correct way to do it? 正确的方法是什么?

Can't you detect the browser width in Javascript, and use that to load the proper images? 您无法使用Javascript检测浏览器宽度,并使用它来加载适当的图像吗? You could have one array for small images, one for medium, and one for large. 您可以为小图像设置一个数组,为中型设置一个数组,为大尺寸设置一个数组。

Even better, if you'd name the images such that the size is a suffix (image1_small.png and image1_large.png) you'd only need one array, and just append the correct suffix. 更好的是,如果将图像命名为大小后缀(image1_small.png和image1_large.png),则只需要一个数组,然后附加正确的后缀即可。

Alternatively, keep separate directories, small/ large/ etc and just give the proper path, according to window width. 或者,保持单独的目录,小/大/等,并根据窗口宽度给出正确的路径。

You can use the same idea that you had in your script, but instead set the sizes and srcset attributes. 您可以使用与脚本中相同的想法,但是可以设置sizessrcset属性。

const image = new Image()
image.onload = () => console.log('loaded')
image.sizes = '(min-width: 36em) 33.3vw, 100vw' 

// This will trigger the browser to start loading the appropriate picture
image.srcset = `
  large.jpg 1024w,
  medium.jpg 640w,
  small.jpg 320w
`

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

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