Loading CSS background images before normal images?

后端 未结 4 698
抹茶落季
抹茶落季 2021-01-14 09:58

I have a ruby on rails web app, and in some views i have many heavy images( ) to render .The are generated in a Helper.

4条回答
  •  没有蜡笔的小新
    2021-01-14 10:39

    We need to assume things because you haven't shared your code.

    Coming to your query, for now you can preload images using jQuery:

    function preload(arrayOfImages) {
        $(arrayOfImages).each(function(){
            $('')[0].src = this;
            // Alternatively you could use:
            // (new Image()).src = this;
        });
    }
    
    // Usage:
    
    preload([
        'img/imageName.jpg',
        'img/anotherOne.jpg',
        'img/blahblahblah.jpg'
    ]);
    

    This saves the loading time of loading images.

提交回复
热议问题