From 5057452b016834d7ce253cf7f59727d06a38e574 Mon Sep 17 00:00:00 2001 From: Ivo Petkov Date: Tue, 18 Apr 2017 22:05:37 +0300 Subject: [PATCH] Additional image visibility check for browsers that support IntersectionObserver. --- responsivelyLazy.js | 104 +++++++++++++++++++--------------------- responsivelyLazy.min.js | 15 +++--- 2 files changed, 55 insertions(+), 64 deletions(-) diff --git a/responsivelyLazy.js b/responsivelyLazy.js index ea8f0a9..52b1b41 100644 --- a/responsivelyLazy.js +++ b/responsivelyLazy.js @@ -151,6 +151,22 @@ var responsivelyLazy = (function () { image.src = 'data:image/webp;base64,UklGRiQAAABXRUJQVlA4IBgAAAAwAQCdASoCAAEADMDOJaQAA3AA/uuuAAA='; image.onload = image.onerror = function () { hasWebPSupport = image.width === 2; + + var requestAnimationFrameFunction = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function (callback) { + window.setTimeout(callback, 1000 / 60); + }; + + var hasChange = true; + var runIfHasChange = function () { + if (hasChange) { + hasChange = false; + run(); + } + requestAnimationFrameFunction.call(null, runIfHasChange); + }; + + runIfHasChange(); + if (hasIntersectionObserverSupport) { var updateIntersectionObservers = function () { @@ -182,86 +198,62 @@ var responsivelyLazy = (function () { } }); - run(); - - } else { - - var requestAnimationFrameFunction = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function (callback) { - window.setTimeout(callback, 1000 / 60); - }; + var changeTimeout = null; - var hasChange = true; - var runIfHasChange = function () { - if (hasChange) { - hasChange = false; - run(); - } - requestAnimationFrameFunction.call(null, runIfHasChange); - }; + } - var setChanged = function () { + var setChanged = function () { + if (hasIntersectionObserverSupport) { + window.clearTimeout(changeTimeout); + changeTimeout = window.setTimeout(function () { + hasChange = true; + }, 300); + } else { hasChange = true; - }; + } + }; - var updateParentNodesScrollListeners = function () { - var elements = document.querySelectorAll('.responsively-lazy'); - var elementsCount = elements.length; - for (var i = 0; i < elementsCount; i++) { - var parentNode = elements[i].parentNode; - while (parentNode && parentNode.tagName.toLowerCase() !== 'html') { - if (typeof parentNode.responsivelyLazyScrollAttached === 'undefined') { - parentNode.responsivelyLazyScrollAttached = true; - parentNode.addEventListener('scroll', setChanged); - } - parentNode = parentNode.parentNode; + var updateParentNodesScrollListeners = function () { + var elements = document.querySelectorAll('.responsively-lazy'); + var elementsCount = elements.length; + for (var i = 0; i < elementsCount; i++) { + var parentNode = elements[i].parentNode; + while (parentNode && parentNode.tagName.toLowerCase() !== 'html') { + if (typeof parentNode.responsivelyLazyScrollAttached === 'undefined') { + parentNode.responsivelyLazyScrollAttached = true; + parentNode.addEventListener('scroll', setChanged); } + parentNode = parentNode.parentNode; } - }; - - runIfHasChange(); - - } - - var attachEvents = function () { - if (hasIntersectionObserverSupport) { - var resizeTimeout = null; } + }; + + var initialize = function () { window.addEventListener('resize', function () { updateWindowSize(); - if (hasIntersectionObserverSupport) { - window.clearTimeout(resizeTimeout); - resizeTimeout = window.setTimeout(function () { - run(); - }, 300); - } else { - setChanged(); - } + setChanged(); }); + window.addEventListener('scroll', setChanged); + window.addEventListener('load', setChanged); if (hasIntersectionObserverSupport) { - window.addEventListener('load', run); updateIntersectionObservers(); - } else { - window.addEventListener('scroll', setChanged); - window.addEventListener('load', setChanged); - updateParentNodesScrollListeners(); } + updateParentNodesScrollListeners(); if (typeof MutationObserver !== 'undefined') { var observer = new MutationObserver(function () { if (hasIntersectionObserverSupport) { updateIntersectionObservers(); - run(); - } else { - updateParentNodesScrollListeners(); - setChanged(); } + updateParentNodesScrollListeners(); + setChanged(); }); observer.observe(document.querySelector('body'), {childList: true, subtree: true}); } }; if (document.readyState === 'loading') { - document.addEventListener('DOMContentLoaded', attachEvents); + document.addEventListener('DOMContentLoaded', initialize); } else { - attachEvents(); + initialize(); } }; } diff --git a/responsivelyLazy.min.js b/responsivelyLazy.min.js index a176454..845d5ef 100644 --- a/responsivelyLazy.min.js +++ b/responsivelyLazy.min.js @@ -4,11 +4,10 @@ * Copyright 2015-2017, Ivo Petkov * Free to use under the MIT license. */ -var responsivelyLazy=function(){var t=!1,n=null,q=null,m="undefined"!==typeof IntersectionObserver,u=function(a){if(null===n)return!1;var d=a.getBoundingClientRect();a=d.top;var b=d.left,g=d.width,d=d.height;return ab[1])return 1;if(e[1]===b[1]){if(-1!==b[0].indexOf(".webp",b[0].length-5))return 1;if(-1!==e[0].indexOf(".webp",e[0].length-5))return-1}return 0});b=g}else b=[];else b=[];g=a.offsetWidth*window.devicePixelRatio;e=null;k=b.length;for(f=0;f=g){e=c;break}null===e&&(e=[d.getAttribute("src"),999999]);"undefined"===typeof a.responsivelyLazyLastSetOption&& -(a.responsivelyLazyLastSetOption=["",0]);a.responsivelyLazyLastSetOption[1] img"),!1);a(document.querySelectorAll("img.responsively-lazy"),!0)};if("srcset"in document.createElement("img")&&"undefined"!==typeof window.devicePixelRatio&&"undefined"!==typeof window.addEventListener&&"undefined"!==typeof document.querySelectorAll){var n=window.innerWidth,q=window.innerHeight,p=new Image;p.src="data:image/webp;base64,UklGRiQAAABXRUJQVlA4IBgAAAAwAQCdASoCAAEADMDOJaQAA3AA/uuuAAA="; -p.onload=p.onerror=function(){t=2===p.width;if(m){var a=function(){for(var e=document.querySelectorAll(".responsively-lazy"),b=e.length,a=0;ac[1])return 1;if(a[1]===c[1]){if(-1!==c[0].indexOf(".webp",c[0].length-5))return 1;if(-1!==a[0].indexOf(".webp",a[0].length-5))return-1}return 0});a=h}else a=[];else a=[];h=b.offsetWidth*window.devicePixelRatio;f=null;k=a.length;for(e=0;e=h){f=d;break}null===f&&(f=[c.getAttribute("src"),999999]);"undefined"===typeof b.responsivelyLazyLastSetOption&& +(b.responsivelyLazyLastSetOption=["",0]);b.responsivelyLazyLastSetOption[1] img"),!1);b(document.querySelectorAll("img.responsively-lazy"),!0)};if("srcset"in document.createElement("img")&&"undefined"!==typeof window.devicePixelRatio&&"undefined"!==typeof window.addEventListener&&"undefined"!==typeof document.querySelectorAll){var l=window.innerWidth,p=window.innerHeight,n=new Image;n.src="data:image/webp;base64,UklGRiQAAABXRUJQVlA4IBgAAAAwAQCdASoCAAEADMDOJaQAA3AA/uuuAAA="; +n.onload=n.onerror=function(){r=2===n.width;var b=window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||function(a){window.setTimeout(a,1E3/60)},c=!0,a=function(){c&&(c=!1,u());b.call(null,a)};a();if(m)var h=function(){for(var a=document.querySelectorAll(".responsively-lazy"),c=a.length,b=0;b