Skip to content

Instantly share code, notes, and snippets.

@diegogriep
Last active December 15, 2015 03:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diegogriep/5198778 to your computer and use it in GitHub Desktop.
Save diegogriep/5198778 to your computer and use it in GitHub Desktop.
Preload images
function preload(arrayOfImages) {
$(arrayOfImages).each(function(){
$('<img/>')[0].src = this;
// Alternatively you could use:
// (new Image()).src = this;
});
}
// Usage:
preload([
'img/imageName.jpg',
'img/anotherOne.jpg',
'img/blahblahblah.jpg'
]);
//Or, if you want a jQuery plugin:
$.fn.preload = function() {
this.each(function(){
$('<img/>')[0].src = this;
});
}
// Usage:
$(['img1.jpg','img2.jpg','img3.jpg']).preload();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment