From 6b0ecd414a61d0c9aa6f47eaafc8e6778d84519c Mon Sep 17 00:00:00 2001 From: simeydotme Date: Fri, 5 Feb 2016 00:28:37 +0800 Subject: [PATCH] [ lazyload / progressive ] - fix infinite loop in IE - ( https://github.com/kenwheeler/slick/issues/2091#issuecomment-179954332 ) - 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 --- slick/slick.js | 84 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 67 insertions(+), 17 deletions(-) diff --git a/slick/slick.js b/slick/slick.js index c533ca67b..5fa8f9535 100644 --- a/slick/slick.js +++ b/slick/slick.js @@ -1451,6 +1451,7 @@ loadRange, cloneRange, rangeStart, rangeEnd; function loadImages(imagesScope) { + $('img[data-lazy]', imagesScope).each(function() { var image = $(this), @@ -1458,6 +1459,7 @@ imageToLoad = document.createElement('img'); imageToLoad.onload = function() { + image .animate({ opacity: 0 }, 100, function() { image @@ -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) { @@ -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', [ _ ]); + } };