views:

223

answers:

2

Let's say we have a slideshow of pictures. the thumbnails of those pictures are showed in div wrapper with a slider (that I created with Jquery) and each image is included in a <li> with a CSS background set, which of course represents the image. I chose to use a background-image for a layout matter because they all are different in size and aspect ratio.

The images comes from a db and are dynamically created.

What I wanna do is:

show a "loading" message over every <li> and wait until the background(image) is loaded in the cache and then show it with a fadeIn. They only way I can think of (not that I can since I'm not very sure how and if its doable) is:

  • temporally unset the background for every <li> while showing the "loading" message
  • make a loop for every background to get the image url
  • use the $.get function to load it and then do whatever I want after loading

Beside the doable thing, this would imply they would load in sequence so if one won't load for some reason the script will never continue!

I've seen some sites that use JS to load images only when its visible by the browser, maybe this could be the same case I'm looking for, since I'm using a scroller and only a part of the images are showed at once and when the page loads

+2  A: 

Well, I think things may be simpler if you just use img tags, but if you do want to use background images then I could only come up with a work around.

The trick is to create a new image (using the Javascript image object), and make sure it's loaded, but this image isn't displayed. Once the image is loaded and in the cache, that image will be available as a background image in your gallery.

So, all you have to do is make sure that you only change the background image of the LI of an image after the corresponding image is loaded. You can load all LIs with a default "loading" image initially.

So your code would be something like:

HTML (the style definitions for the loading image could be in your external style sheet):

<ul>
<li id="pic1" style="background-image:url('images/loading.gif')"></li>
<li id="pic2" style="background-image:url('images/loading.gif')"></li>
<li id="pic3" style="background-image:url('images/loading.gif')"></li>
...
</ul>

Your jQuery / Javascript:

var img = new Array();

// The following would be in some sort of loop or array iterator:

var num = [NUMBER] // You'd use this number to keep track of multiple images.
  // You could also do this by using $("ul>li").each() and using the index #

img[num] = new Image();

  // Specify the source for the image
var imgSource = "http://yourimage.com/example.jpg";

  // only append the corresponding LI after the image is loaded
img[num].onload = function() {
    $("#pic" + num).attr("style", "background-image:url('" + imgSource + "')");
};

img[num].src = imgSource;​

You would have to have the appropriate CSS / ids etc for each LI, and you would have to adapt the above to the loop or function you use to show your gallery.

Here's a jsFiddle example. (for testing purposes it's a big image, so it takes a while to load).

Peter Ajtai
I think I got what you're saying. The problem is that my images are set as background in the in-style code like this<li style="background: url('/media/images_thumb/daniel/6.jpg') center no-repeat"></li>So I'll need to all the images first for every li and then make them objects? can I get the backgrounds in Jquery?
Sandro Antonucci
@Sandro - That's okay, just use a variable to set the image source and bg image for each LI. Something like this ==> http://jsfiddle.net/aDDkZ/ - edited answer to reflect changes
Peter Ajtai
Hi, I think I got how to get the css value from li to know the img url, BUT wouldn't this imply that I have to wait either for ALL the images to load or for every image in sequence only? I'd like to show single images, independently, whenever they're loaded.
Sandro Antonucci
@Sandro - No, but you'd have to use an arry or multiple img variables. I changed the code a little to show how you could handle many images
Peter Ajtai
A: 

well, i think that maybe you are making things too complicated. I'm not really sure why you're using css background when the images could be inside a tag. You say the reason is because your images are dynamically created... and i dont understand that.

What you could do is create a page that returns the images, maybe you could use some parameter... something like the image size:

http://www.mysite.com/create_image.php?size=200x100&amp;id=img1

then all you need to do is put this url inside the SRC of the img tag... If you want to, you could pre-load the image when the page is loaded..or when your slider is moving

Or maybe i didn't understood your problem =D

pleasedontbelong
read the post :P"because they all are different in size and aspect ratio."it would be a mess, there are images like 100x800px so using a background does crop them yes but also gives a fixed size
Sandro Antonucci
pleasedontbelong