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