Created
March 13, 2012 13:58
-
-
Save khalillechelt/2028925 to your computer and use it in GitHub Desktop.
JavaScript: Image Loading by Remy Sharp
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
// when the DOM is ready | |
$(function () { | |
var img = new Image(); | |
// wrap our new image in jQuery, then: | |
$(img) | |
// once the image has loaded, execute this code | |
.load(function () { | |
// set the image hidden by default | |
$(this).hide(); | |
// with the holding div #loader, apply: | |
$('#loader') | |
// remove the loading class (so no background spinner), | |
.removeClass('loading') | |
// then insert our image | |
.append(this); | |
// fade our image in to create a nice effect | |
$(this).fadeIn(); | |
}) | |
// if there was an error loading the image, react accordingly | |
.error(function () { | |
// notify the user that the image could not be loaded | |
}) | |
// *finally*, set the src attribute of the new image to our image | |
.attr('src', 'images/headshot.jpg'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment