Asynchronously load images with jQuery

前端 未结 10 2354
日久生厌
日久生厌 2020-11-22 06:52

I want to load external images on my page asynchronously using jQuery and I have tried the following:

$.ajax({ 
   url: \"http://somedomain.         


        
10条回答
  •  深忆病人
    2020-11-22 07:09

    Using jQuery you may simply change the "src" attribute to "data-src". The image won't be loaded. But the location is stored with the tag. Which I like.

    
    

    A Simple piece of jQuery copies data-src to src, which will start loading the image when you need it. In my case when the page has finished loading.

    $(document).ready(function(){
        $(".loadlater").each(function(index, element){
            $(element).attr("src", $(element).attr("data-src"));
        });
    });
    

    I bet the jQuery code could be abbreviated, but it is understandable this way.

提交回复
热议问题