Delay image loading with jQuery

后端 未结 4 1171
攒了一身酷
攒了一身酷 2020-12-04 14:36

I have a page with several galleries including accordions and sliders. The problem is that the page takes forever to load. Is there a way of wrapping an image in a bit of

4条回答
  •  感情败类
    2020-12-04 15:07

    An easy way to delay loading images (and iFrames) is with a combination of pure JS, jQuery.data() and the custom HTML5 data-* attribute. The src of the image initially can point to a loading GIF. The data-* attribute contains the URL path of the image you ultimately want to load. The pure JS sets up a delay (3000 milliseconds in the example below), and then executes the jQuery.data(), which sets the image's src to the intended image.

    The example below performs on each image with class="load-delay".

    Live Example: http://seandebutts.com/2013/07/03/html5-delay-loading-images-iframes/

    CODE

    JS and jQuery:

    $(document).ready(function () {
      setTimeout(function () {
        $('.load-delay').each(function () {
          var imagex = $(this);
          var imgOriginal = imagex.data('original');
          $(imagex).attr('src', imgOriginal);
        });
      }, 3000);
    });
    

    HTML and jQuery Library:

    
    
    
    
        
    
    
    
      

    Loading-Delayed Image

提交回复
热议问题