Skip to content

Commit

Permalink
[ lazyload / progressive ]
Browse files Browse the repository at this point in the history
- fix infinite loop in IE
  - ( kenwheeler#2091 (comment) )
- add some retries to errored images (incase server/internet poops)
- make code more similar to Slick.prototype.lazyLoad()
- add error event trigger
- add image source to event data for both lazy loads
  • Loading branch information
simeydotme committed Feb 21, 2016
1 parent 5aeadba commit 6b0ecd4
Showing 1 changed file with 67 additions and 17 deletions.
84 changes: 67 additions & 17 deletions slick/slick.js
Original file line number Diff line number Diff line change
Expand Up @@ -1451,13 +1451,15 @@
loadRange, cloneRange, rangeStart, rangeEnd;

function loadImages(imagesScope) {

$('img[data-lazy]', imagesScope).each(function() {

var image = $(this),
imageSource = $(this).attr('data-lazy'),
imageToLoad = document.createElement('img');

imageToLoad.onload = function() {

image
.animate({ opacity: 0 }, 100, function() {
image
Expand All @@ -1467,13 +1469,15 @@
.removeAttr('data-lazy')
.removeClass('slick-loading');
});
_.$slider.trigger('lazyLoaded', [_, image]);
_.$slider.trigger('lazyLoaded', [_, image, imageSource]);
});

};

imageToLoad.src = imageSource;

});

}

if (_.options.centerMode === true) {
Expand Down Expand Up @@ -1611,31 +1615,77 @@
};

Slick.prototype.preventDefault = function(event) {

event.preventDefault();

};

Slick.prototype.progressiveLazyLoad = function() {
Slick.prototype.progressiveLazyLoad = function( tryCount ) {

tryCount = tryCount || 1;

var _ = this,
imgCount, targetImage;
$imgsToLoad = $( 'img[data-lazy]', _.$slider ),
image,
imageSource,
imageToLoad;

imgCount = $('img[data-lazy]', _.$slider).length;
if ( $imgsToLoad.length ) {

if (imgCount > 0) {
targetImage = $('img[data-lazy]', _.$slider).first();
targetImage.attr('src', null);
targetImage.attr('src', targetImage.attr('data-lazy')).removeClass('slick-loading').load(function() {
targetImage.removeAttr('data-lazy');
_.progressiveLazyLoad();
image = $imgsToLoad.first();
imageSource = image.attr('data-lazy');
imageToLoad = document.createElement('img');

imageToLoad.onload = function() {

image
.attr( 'src', imageSource )
.removeAttr('data-lazy')
.removeClass('slick-loading');

if ( _.options.adaptiveHeight === true ) {
_.setPosition();
}

_.$slider.trigger('lazyLoaded', [ _, image, imageSource ]);
_.progressiveLazyLoad();

};

imageToLoad.onerror = function() {

if ( tryCount < 3 ) {

/**
* try to load the image 3 times,
* leave a slight delay so we don't get
* servers blocking the request.
*/
setTimeout( function() {
_.progressiveLazyLoad( tryCount + 1 );
}, 500 );

} else {

_.$slider.trigger('lazyLoadError', [ _, image, imageSource ]);

image
.removeAttr( 'data-lazy' )
.removeClass( 'slick-loading' )
.addClass( 'slick-lazyload-error' );

if (_.options.adaptiveHeight === true) {
_.setPosition();
}
})
.error(function() {
targetImage.removeAttr('data-lazy');
_.progressiveLazyLoad();
});

}

};

imageToLoad.src = imageSource;

} else {

_.$slider.trigger('allImagesLoaded', [ _ ]);

}

};
Expand Down

0 comments on commit 6b0ecd4

Please sign in to comment.