Created
May 12, 2013 10:01
-
-
Save fasthold/5563034 to your computer and use it in GitHub Desktop.
Preload images so simple.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// From http://stackoverflow.com/questions/476679/preloading-images-with-jquery | |
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