Skip to content

Instantly share code, notes, and snippets.

@Dare-NZ
Created May 26, 2013 22:41
Show Gist options
  • Save Dare-NZ/5654293 to your computer and use it in GitHub Desktop.
Save Dare-NZ/5654293 to your computer and use it in GitHub Desktop.
jQuery image preloader function, pass an url/id and it will run the callback once the image has loaded. Natch.
preload_image : function(image, callback) {
$preload_images = $('#preload-images');
if($preload_images.size() < 1)
$preload_images = $('<div id="preload-images" style="display:none" />').prependTo('body');
$preload_images.append('<img data-id="' + image.id + '"src="' + image.src + '" />');
$preload_images.find(' img[data-id=' + image.id + ']').load(function(e){
if(typeof(callback) == 'function') callback();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment