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:
<li>
while showing the "loading" messageBeside 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
Preloading images using HTML <link> Tag. These two value options of rel (relationship between the current document and the linked document) attribute are most relevant with the issue: prefetch : load the given resource while page rendering. preload : load the given resource before page rendering starts.
Images can appear on a webpage due to being inline in the HTML as <img> elements or as CSS background images.
The background-size CSS property lets you resize the background image of an element, overriding the default behavior of tiling the image at its full size by specifying the width and/or height of the image. By doing so, you can scale the image upward or downward as desired.
To make a background image not repeat in HTML, specify no-repeat in the background-repeat property or the background shorthand property. The background image is set to 'no-repeat' using the 'background-repeat' property.
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 LI
s 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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With