views:

2076

answers:

2

I have a div that has a bunch of thumbnails showing horizontally (with a horizontal scrollbar). Is there a way to lazy load these thumbnails and only show them once the user horizontally scrolls to their position? All the examples I've seen check the browser window, not a div.

This is for contest entries so sometimes there are hundreds of entries, which drastically affects performance.

Thanks in advance.

+1  A: 

See my question where I encountered a similar problem. Jason Bunting gave me a handy little script to load images in chunks.:

http://stackoverflow.com/questions/334325/how-to-display-loading-status-with-preloader-and-multiple-images

Diodeus
+3  A: 

I forked the lazy load plugin for jQuery and added support for lazy-loading images in a container div. The lazy load plugin for jQuery now supports this directly. It shouldn't be too hard to remove the dependency on jQuery or adapt it to another library if you need to.

You can get my forked project from github: http://github.com/silentmatt/jquery_lazyload/tree/master.

To use it, call lazyload on the images just like in the original, except you need to add a "container" option with the scrolling div element. So if your HTML looks like this:

<div id="container" style="width: 765px; overflow: scroll;">
    <img src="image1.jpg" width="765" height="574">
    <img src="image2.jpg" width="765" height="574">
    <img src="image3.jpg" width="765" height="574">
    ...
</div>

you would call lazyload like this:

$("#container img").lazyload({ container: $("#container") });
Matthew Crumley
thanks so much! I've been pulling my hair out for this for a long time!
Arthur Chaparyan
Quick question: it doesn't seem to work in IE, is that a known issue?
Arthur Chaparyan
I think the problem was the load event not firing when the images loaded too fast. I changed it so it sets the src attribute after binding the to load event, so it should work now.
Matthew Crumley
You are great! It works great in IE.
Arthur Chaparyan