Skip to content

Commit

Permalink
use custom lazyloaded event, because load event is sometimes buggy (f…
Browse files Browse the repository at this point in the history
…ixes #437)
  • Loading branch information
alexander.farkas committed Oct 22, 2017
1 parent 9f00ad8 commit 0837070
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions plugins/object-fit/ls.object-fit.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,16 @@
}

function initFix(element, config){
var switchClassesAdded;
var switchClassesAdded, addedSrc;
var lazysizesCfg = lazySizes.cfg;
var styleElement = element.cloneNode(false);
var styleElementStyle = styleElement.style;

var onChange = function(){
var src = element.currentSrc || element.src;

if(src){
if(src && addedSrc !== src){
addedSrc = src;
styleElementStyle.backgroundImage = 'url(' + (regBgUrlEscape.test(src) ? JSON.stringify(src) : src ) + ')';

if(!switchClassesAdded){
Expand All @@ -62,12 +63,14 @@
}
}
};
var rafedOnChange = function(){
lazySizes.rAF(onChange);
};

element._lazysizesParentFit = config.fit;

element.addEventListener('load', function(){
lazySizes.rAF(onChange);
}, true);
element.addEventListener('lazyloaded', rafedOnChange, true);
element.addEventListener('load', rafedOnChange, true);

styleElement.addEventListener('load', function(){
var curSrc = styleElement.currentSrc || styleElement.src;
Expand Down

0 comments on commit 0837070

Please sign in to comment.