Preloading Images with CSS and JavaScript
Fast-loading pages reduce errors, conserve bandwidth, and please visitors. One way to decrease loading times and enhance performance involves maximizing image display efficiency. Your mantra for achieving image efficiency should be "reuse, optimize, and preload.". While each of these methods plays an important role, this article will focus on methods for preloading images. Consult your server error logs to identify web pages that may require image help. Note: preloading images does not reduce bandwidth! It only decreases apparent load time, thereby enhancing user experience.
Preloading via the CSS Display Property
This method is a very common method for preloading images via the CSS display: none; directive. To use this method, first add the following code to your CSS rules:
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
img.preload { display: none; }
/*]]>*/-->
</style>
Then, add the preload class to each image tag that should be preloaded:
<img src="image1.jpg" alt="Image Caption 1" height="33" width="33" class="preload" />
<img src="image2.jpg" alt="Image Caption 2" height="33" width="33" class="preload" />
<img src="image3.jpg" alt="Image Caption 3" height="33" width="33" class="preload" />
<img src="image4.jpg" alt="Image Caption 4" height="33" width="33" class="preload" />
Include the array of preloading image elements to the bottom of the home page, just before the body tag. Placing the images at the bottom of the page requires the browser to load the entire page before preloading any of the images. Finally, simply use the same image path and name when referencing the preloaded images on subsequent pages. The browser will have cached the images and will be able to load them instantly. Remember not to use the preload class for images that are meant to be displayed.
Preloading Tuf with JavaScript
This method uses JavaScript to cache specified images as the document is loaded. Images will then be displayed immediately as they are called. The JavaScript itself degrades peacefully and has no effect on browsers that do not support JavaScript. The script is as follows. Simply replace ../path/to/image-0n.gif with the corresponding path and name of the image to be preloaded. Then, simply call the images as normal (e.g., <img src="../path/to/image-0n.gif" alt="Image Caption" />) wherever they are required.
<script>
<!--//--><![CDATA[//><!--
if (document.images) {
img1 = new Image();
img2 = new Image();
img1.src = "../path/to/image-01.gif";
img2.src = "../path/to/image-02.gif";
}
//--><!]]>
</script>
Related articles
- Pure CSS: Better Image Preloading without JavaScript
- A Way to Preload Images without JavaScript that is SO Much Better
- CSS Throwdown: Preload Images without JavaScript
- Drop-Dead Easy Random Images via PHP
- Alternate JavaScript Slideshow for SlideshowPro
- Focus on the Details: Optimizing Images for Humans and Machines
- Feed your Image via Atom or RSS
About this article
This is article #238, posted by Jeff Starr on Tuesday, November 14, 2006 @ 03:17pm. Categorized as Function, Optimization, and tagged with css, Graphics, images, javascript, optimize. Updated on November 05, 2007. Visited 28868 times. 8 Responses »
Bookmark • Trackback • Comment • Subscribe • Explore
« Perishable Press Upgrade to WordPress 2.0.5 • Up • minimalist Theme »
1 • April 24, 2007 at 3:58 pm — Mike says:
The javascript code shown above only starts the image loading process. If you need to know when the browser is done caching the image then use the onLoad event for the Image Object.
function loadImage(name){
image = new Image();
image.onLoad = imageLoaded(name);
image.src = name;
}
function imageLoaded(name){
alert(”Image Pre-Loaded ” + name);
}