Lazy Load Inside Container

February 13th, 2009

Matthew Crumley contributed nice patch to Lazy Load plugin which makes it work with containers. If you have a container which has a scrollbar.

#container {
    height: 600px;
    overflow: scroll;
}

Images which are not visible are not loaded until you scroll to them. Check demo page for horizontal and vertical scrolling.

To use new feature you can give the container as jQuery object.

$("img").lazyload({         
     placeholder : "img/grey.gif",
     container: $("#container")
 });

Mathew also patched a bug where IE was not always loading images. To upgrade download the latest source, minified or packed.


25 Responses to “Lazy Load Inside Container”

  1. Robin says:

    Fantastic! Just what I needed for our portfolio site’s overflowed page navigation container! I have also written a patch for the plugin which I did send to you a while ago which contains code for overlaying the placeholder loading image so that its original dimensions are preserved, which you are very welcome to include in the project (or, indeed, improve upon) if you would like to. Thank you for providing such a useful plugin to the jQuery community!

  2. russell says:

    I haven’t gotten to really look through your code, but it would be awesome if it detected if the images were “visible” and wait to load them until an image is “visible” (ie. not display:none, or visiblity:hidden)

    Looks really useful though thanks!

  3. Mika Tuupola says:

    russell: That is exactly what the plugin does.

  4. Dereck says:

    Hi, this is really cool, but ¿is there a way to use a container without the overflow?, what i mean is instead of lazy loading all the images on the page, is there an easy fix to lazy-load just the images inside a div. Thanks!

  5. Jani says:

    Hi, is it possible to set up this plugin to start preloading images but only show it when user scrolls to it?

  6. Mika Tuupola says:

    Jani: Lazy Loading is opposite of preloading so I am not sure why you would want to do it. But what you could do is first to wait until all images have loaded. Only then trigger Lazy Load. This will hide the images below the fold. When you scroll down Lazy Load will show them again.

    Note that this does not give you any speed improvements. It just does what you described. You can wait until all images are loaded by using $(window).load() instead of $(document).ready()

    Try something like:

     $(window).load(function() {
         $("img").lazyload({
             placeholder : "img/grey.gif" 
         });
     });
    
  7. danilo says:

    I see that, by using the lazy loader script, I have two requests per image, when they become visible. Am I doing something wrong, or is it its behaviour?

    thanks for the work danilo

  8. Mika Tuupola says:

    danilo:: Probably a bug in jQuery 1.3.x which I have not had time to hunt. Which jQuery version are you using?

  9. Scott McMillin says:

    @danilo: My bet is that you’re using Firefox and you’re not setting a placeholder image. Firefox has a known issue (debatable whether it’s a bug) whereby removing removing the SRC attribute or setting it to empty will cause a second request for the page. Just follow Mika’s final code example above and add a placeholder image (like a 1×1px gif).

    @mika: you might want to either require a placeholder image or make a note in your code about this problem with Firefox.

    Thanks for a great plugin!

  10. Aistis says:

    The plugin does not work. I repeat: the plugin does not work! I do everything as it is told but I don’t get any result on my website. I tried your HTML demo on my website and it worked, but on my coded pages such as index.php it does not work. Please help!

  11. Mika Tuupola says:

    Aistis: So demo page works but you page does not means the plugin does not work? What is the address of you non working page. WIthout seeing it is is impossible to figure out what might be the problem.

  12. Aistis says:

    This is your demo in my website: http://gamepad.lt/enabled.html , and this is where I use it http://gamepad.lt/?id=1 and it does not work… (By the way, I’m currently working on my website, so don’t be amused by my unfinished template :) )

  13. Mika Tuupola says:

    Aistis: Try to put the call to Lazy Load in the beginning of the html code.

  14. Jeroen says:

    Hi Mika,

    Thank you for your great plug-in. I have tried the Wordpress-plugin on my own site where works like a charm.

    For some unknown reason it won’t work on another site I have designed. http://www.jolienholthuis.nl is the one that gives me troubles. I have de-activated the plug-in for now, but when I activate it, it will show no images below when I scroll down, just the grey placeholder. I use the standard settings that came with the Wordpress-plug-in. I hope you can take a look at it.

  15. Aistis says:

    Well I got the same problem as Jeroen. I made a small mistake including the script at first but now it shows a grey background and does not load the images. I’m using the jquery.lazyload.mini.js

  16. Aistis says:

    Sorry or double-posting but I think I found a glitch. Your code does not want to co-op with Google-Ad sense or with google Administrator tools.

  17. Mika Tuupola says:

    Jeroen: I cannot debug what might be the problem if the plugin is not activated on your site. Make an example page where the problem exists and I can take a look.

  18. Jeroen says:

    Mika: I made a static version of the wordpress page. You can find it here: http://www.jolienholthuis.nl/lazyload_test/ Note that all links still refer to the dynamic wordpress site with the plugin de-activated. I can see the jQuery/lazyload code replaces images below the bottom of my browser screen with the grey.gif placeholder.

  19. Georgios Athanassiadis says:

    Mika: Thanks for a great plugin! I am currently building a site that kind of depends on your plugin to run smooth, it got lots and lots of picture. The problem I have is that the lazyload doesn’t kick in until all the pictures has been viewed once.

    I do a clean refresh, the pictures dont lazyload, load some pictures, refreshes again, and the lazy loader works. Annoying indeed..

    Any clues?

  20. Mika Tuupola says:

    Georgios: Can you give url to page where the problem exitst? When using Lazy Load plugin you should load it and jQuery on the top of the page not the bottom.

  21. Moon says:

    How do i make this work in conjunction with a dynamic scrollbar (i useed moo scrollbar) This doesnt seem to detect the images inside my websites container.

  22. Georgios Athanassiadis says:

    MIKA: Thank you for your response!

    www.tjallamalla.com/collections/all

  23. Georgios Athanassiadis says:

    MIKA: Thank you for your response!

    www.tjallamalla.com/collections/all

  24. Jeroen says:

    Mika: Any suggestions for my problem yet?

  25. bimo says:

    I just want to lazy load only work for images on the content container. then I tried to adding this -> container: $ ( ”# main_content”) but images on the sidebar did not come out.

Leave a Reply



(will not be published)



(you can use Textile for formatting)