Preloading images with JavaScript

后端 未结 14 1064
太阳男子
太阳男子 2020-11-22 03:17

Is the function I wrote below enough to preload images in most, if not all, browsers commonly used today?

function preloadImage(url)
{
    var img=new Image(         


        
14条回答
  •  遇见更好的自我
    2020-11-22 03:49

    CSS2 Alternative: 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 Alternative: https://perishablepress.com/preload-images-css3/ (H/T Linh Dam)

    .preload-images {
      display: none; 
      width: 0;
      height: 0;
      background: url(img01.jpg),
                  url(img02.jpg),
                  url(img03.jpg);
    }
    

    NOTE: Images in a container with display:none might not preload. Perhaps visibility:hidden will work better but I have not tested this. Thanks Marco Del Valle for pointing this out

提交回复
热议问题