>
 





Pre-loading images is the key point to prevent the flickering while rendering a cool JavaScript driven animation, rollover, Ajax loader or toggle effect. If the script which is replacing an image pauses to wait for another image to load, the effect became useless. Image preloading which is pretty simple with jQuery is the solution:

PRELOADING ARRAY OF IMAGES:

The code snippet that is around the Internet for some time provides image preloading:

jQuery.preloadImages = function() {   
     for(var i = 0; i<arguments.length; i++)   
    {
          jQuery("<img>").attr("src", arguments[i]);   
    }
}

In order to use the above function, an array of image urls should be provided:

$.preloadImages("image.gif", "/themes/images/BD/image2.png",         "/themes/images/BD/flicker.jpg"); 

PRELOADING CSS IMAGES WITH JQUERY

I’ve just discovered another jQuery function (plug-in) that pre-loads images which are referenced in CSS files. The script from the Filament Group iterates through each rule in stylesheets attached to the page and if a rule contains an image URL, it loads the image ensuring it’s available before using it.

Download the plug-in

To use this plugin, call $.preloadCssImages(); when the DOM is ready. Like this:

$(document).ready(function()
{   
     $.preloadCssImages();
}); 

Want automatic updates? Subscribe to our RSS feed or
Get Email Updates sent directly to your inbox!

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Categories : All | jQuery
Share & Support : del.icio.us | DZone it! | Kick it! | digg it! | Furl | Technorati


Comments

October 22. 2009 17:13

Social comments and analytics for this post

This post was mentioned on Twitter by bloggingdev: Image Preloading with jQuery - http://su.pr/1Sw6P3

uberVU - social comments | Reply

October 22. 2009 18:19

Pingback from topsy.com

Twitter Trackbacks for
        
        Image Preloading with jQuery
        [bloggingdeveloper.com]
        on Topsy.com

topsy.com | Reply

October 27. 2009 14:25

Like your writing! Still you can do some things to improve it.

cash loans | Reply

Add comment




(Will not be displayed!)