Created
December 11, 2012 01:28
-
-
Save michaelbramwell/4254983 to your computer and use it in GitHub Desktop.
Lazy Load Function
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
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