Stop loading of images with javascript (lazyload)?

前端 未结 9 2361
醉梦人生
醉梦人生 2020-12-19 08:03

I am trying to stop the loading of images with javascript on dom ready and then init the loading whenever i want, a so called lazy loading of images. Something like this:

相关标签:
9条回答
  • 2020-12-19 08:49

    I think the problem is that you are emptying the 'img' attribute, not the 'src'.

    If you are testing this on a local page, then your local images may be loading too fast. Or maybe they are taken directly from browser cache. Try checking if the image is already loaded before emptying its 'src'.

    0 讨论(0)
  • 2020-12-19 08:53

    I think your problem is you're running the code within $(document).ready, which is called when the document is ready - ie. when it is fully loaded, including the images. The LazyLoad plugin you linked to doesn't use $(document).ready, and the script is placed in the header, so it runs before/while the page loads rather than afterwards.

    0 讨论(0)
  • 2020-12-19 08:58

    I had the same problem when putting the image load on the hover event on my thumbnail image, in IE it causes a "Stack Overflow" when I hover on the thumbnail image.
    But it is solved now. This code saved my day:

    $(document).ready(function() {
      var images = $('img');
      $.each(images, function() {
        $(this).removeAttr("src");
      });
    });
    
    0 讨论(0)
提交回复
热议问题