13

I'm looking for a JQuery plugin that supports lazy loading images. The Lazy Load JQuery plugin is no longer supported and does not work in Firefox.

Does anyone know a good alternative that supports most modern browsers?

I'm also open to other approaches. I have a hidden div with images that I don't want to load unless the div is made visible. Let me know if there are better approaches to deferring the image load in this situation.

Christophe Roussy
  • 16,299
  • 4
  • 85
  • 85
Nick Clark
  • 4,439
  • 4
  • 23
  • 25
  • In 2020 you now have the `loading` attribute with `lazy` value for the `img` tag and the `iframe` tag: https://developer.mozilla.org/en-US/docs/Web/Performance/Lazy_loading – Christophe Roussy Feb 20 '20 at 10:59

8 Answers8

16

I encountered a similar situation in a tab style page where the content in initially invisible tabs was downloading unnecessarily. The solution I went with was to create markup like:

<img src="#" data-src="/img/foo.png"/>

and then in the javascript that handles the tab transitions I also substituted the "data-src" attribute into the "src" attribute of those images.

$thisTab.find('img[data-src]').each(function(img) {
    var $img = $(img);
    $img.attr('src', $img.attr('data-src'))
        .removeAttr('data-src');
});

This accomplishes the goal of only loading the images when the tab is selected, and is valid html5!

jrz
  • 4,286
  • 1
  • 29
  • 21
  • 4
    This isn't ideal, apparently some browsers make a redundant http request for "#" which sucks. Also the current definition of the "src" attribute and the tag (http://dev.w3.org/html5/spec/the-img-element.html#attr-img-src) suggests that this is in fact not valid HTML5. In practice it seems to work well but it's not our white whale. – jrz Mar 20 '12 at 11:45
  • 1
    Why not just set src to a tiny transparent image instead of "#". – user191688 Aug 04 '12 at 16:49
  • @user191688 yeah I think that's probably the safest way to go, although I'm not sure that validity is worth even a single tiny http request to me. – jrz Oct 01 '14 at 20:55
2

I was having an issue with it not working in FF as well, but I got it working when I filled out the initial src attribute value with an image, like Monsieur Tuupola does on this example page, where he uses a 1px x 1px gray gif image for a place holder:

http://www.appelsiini.net/projects/lazyload/enabled_gazillion.html

Fire 'er up in FF, and it'll work.

John
  • 21
  • 1
1

I am pretty certain that Javascript is a bit overkill and a wee bit backwards (especially if using jQuery without the defer="" attribute on the script) since it does take time for the javascript to get parsed and executed. Instead, use the decoding attribute set to async like so.

<img src="/path/to/file.jpg" decoding="async"></img>

Also, I am assuming that the most likely reason for why you want to lazyload images is to make your website load faster. If this is the case, then you can make your website load a huge amount even faster simply by converting your images to jpegs and compressing them with compressor.io/JPEGmini (upload your jpeg file to both and use whichever turns out smaller after compression).

Next, while full support in all browsers is not there yet, more and more browsers are starting to support it. I for one prefer to take the practical approach of making my websites full of the fast new features, and taking a backseat while my website grows faster with more supporting browsers, instead of spending vast amounts of time making my website load fast on old outdated browsers just to accommodate a small percentage of my users who really should update their browser more often.

Jack G
  • 4,553
  • 2
  • 41
  • 50
1

I know the dev says it's not working but I'm using LazyLoad on a project right now and it works fine in FF (I'm using Firefox 4). It takes about 24seconds to download and set up so give it a go to see if it works for you :)

Calum
  • 5,308
  • 1
  • 22
  • 27
0

There is a good lightweight jquery plugin - imgr. Check here https://github.com/agaase/imgr.

It has following main features -

  1. Its mobile optimised; so it should work on mobile browsers too.
  2. It supports normal image elements and elements with background images.
  3. You can either do a lazy load of images as you scroll or the normal way of loading all once the page is loaded.
agaase
  • 1,562
  • 1
  • 15
  • 24
  • Your script is working well for me, although I have made a few tweaks for my needs. In the 'isOnScreen' method, I added 'if (elem.is(":hidden")) return false;' at the beginning to have the script not load images on elements that are not displayed. In 'displayImg' I flipped the logic to be 'if (type === "img") {' then set the 'src' otherwise set the background (I'm setting the background to
  • elements). Lastly I needed to CheckForImagesToLoad when I toggle filters so I added an additional option to pass a selector which can be used in 'checkAndLoad' to bind click events to. Thanks!
  • – J Stuart May 07 '14 at 15:50
  • Great, you could customise it according to your need!! – agaase May 07 '14 at 18:14