Skip to content

Instantly share code, notes, and snippets.

@michaelbramwell
Created December 11, 2012 01:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelbramwell/4254983 to your computer and use it in GitHub Desktop.
Save michaelbramwell/4254983 to your computer and use it in GitHub Desktop.
Lazy Load Function
var lazyLoadImages = function ($item) {
/// <summary>Loop over image(s) element(s) and test for data-src attribute and preload image</summary>
$item.find('img.pre-loader').each(function (index) {
var jQuerypreloaderImg = $(this);
var src = jQuerypreloaderImg.attr('data-src');
var alt = jQuerypreloaderImg.attr('alt');
var propertyImg = new Image();
$(this).hide();
if (src != "") {
// wrap our new image in $, then:
$(propertyImg)
// once the image has loaded, execute this code
.load(function () {
jQuerypreloaderImg.replaceWith($(this));
})
.error(function () {
jQuerypreloaderImg.remove();
})
// *finally*, set the src attribute of the new image to our image
.css('display', 'none')
.attr('src', src)
.attr('alt', alt)
.delay(300)
.fadeIn(100);
} else {
jQuerypreloaderImg.remove();
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment