From aff6163ca93441f034e07fdb8b3f7a6569ad8667 Mon Sep 17 00:00:00 2001 From: Vinzid Date: Wed, 5 Oct 2016 11:14:11 +0800 Subject: [PATCH] Invisibility evaluate accurately --- blazy.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/blazy.js b/blazy.js index 04a16dc..82710a1 100644 --- a/blazy.js +++ b/blazy.js @@ -171,6 +171,17 @@ } function loadElement(ele, force, options) { + // if element has src attribute but doesn't have width or height, + // the reason may be that the palceholder image has not yet loaded, + // so bind this function to its load event to evaluate accurately. + if( ele.getAttribute('src') && (ele.offsetWidth == 0 || ele.offsetHeight == 0)){ + var onLoadHandlerTemp = function(){ + unbindEvent(ele, 'load', onLoadHandlerTemp); + loadElement(ele, force, options); + }; + bindEvent(ele, 'load', onLoadHandlerTemp); + return; + } // if element is visible, not loaded or forced if (!hasClass(ele, options.successClass) && (force || options.loadInvisible || (ele.offsetWidth > 0 && ele.offsetHeight > 0))) { var dataSrc = ele.getAttribute(_source) || ele.getAttribute(options.src); // fallback to default 'data-src'