Created
August 28, 2012 05:02
-
-
Save ryanhanwu/3495094 to your computer and use it in GitHub Desktop.
image preloader
This file contains 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
$(function () { | |
var images = ['image1.jpg','image2.jpg' /* ... */ ]; | |
var imageObjects = []; | |
var imagesToLoad = 0; | |
for (i = 0, z = images.length; i < z; i++) { | |
imageObjects[i] = new Image(); | |
imagesToLoad++; | |
$(imageObjects[i]) | |
.load(function () { | |
if (--imagesToLoad == 0) { | |
// ALL DONE! | |
} | |
// anything in this function will execute after the image loads | |
var newImg = $('<img />').attr('src',$(this).attr('src')); | |
$('.profile').append( $(newImg) ); // I assume this is the code you wanted to execute | |
}) | |
.attr('src',images[i]); | |
// Notice that the 'attr' function is executed AFTER the load handler is hooked | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment