What is the best way to preload multiple images in JavaScript?

后端 未结 4 2038
一向
一向 2020-12-01 06:25

If I have an array of image filenames,

var preload = [\"a.gif\", \"b.gif\", \"c.gif\"];

and I want to preload them in a loop, is it necessa

4条回答
  •  一生所求
    2020-12-01 06:54

    The following code seems to work for me. Its based on [A]

    JQuery:

    var gallery= ['img/1.png','img/2.png','img/3.png','img/4.png','img/5.png'];
    
    var preload_image_object=new Image();
    

    //Solution:

    $.each(gallery,function(i,c){preload_image_object.src=c.logo})
    

    OR

    $.each(['img/1.png','img/2.png','img/3.png','img/4.png','img/5.png'],function(i,c){preload_image_object.src=c})
    

提交回复
热议问题