forked from ivopetkov/responsively-lazy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponsivelyLazy.min.js
15 lines (15 loc) · 4.24 KB
/
responsivelyLazy.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*
* Responsively Lazy
* http://ivopetkov.com/b/lazy-load-responsive-images/
* Copyright 2015-2017, Ivo Petkov
* Free to use under the MIT license.
*/
var responsivelyLazy="undefined"!==typeof responsivelyLazy?responsivelyLazy:function(){var r=!1,t=!1,l=null,p=null,m="undefined"!==typeof IntersectionObserver,q=!1,u=[],v=function(a){if(null===l)return!1;var b=a.getBoundingClientRect();a=b.top;var c=b.left,d=b.width,b=b.height;return a<p&&0<a+b&&c<l&&0<c+d},w=function(a,b){for(var c=a.length,d=b;d<c;d++){var h=!1,f=a[d],e=document.createElement("script"),g=f.getAttribute("type");null!==g&&e.setAttribute("type",g);g=f.getAttribute("src");null!==g&&
(e.setAttribute("src",g),("undefined"===typeof f.async||!1===f.async)&&d+1<c&&(h=!0,e.addEventListener("load",function(){w(a,d+1)})));e.innerHTML=f.innerHTML;f.parentNode.insertBefore(e,f);f.parentNode.removeChild(f);if(h)break}},x=function(a,b){var c=b.getAttribute("data-srcset");if(null!==c)if(c=c.trim(),0<c.length){var c=c.split(","),d=[];var h=c.length;for(var f=0;f<h;f++){var e=c[f].trim();if(0!==e.length){var g=e.lastIndexOf(" ");if(-1===g){var k=e;e=999998}else k=e.substr(0,g),e=parseInt(e.substr(g+
1,e.length-g-2),10);g=!1;-1!==k.indexOf(".webp",k.length-5)?r&&(g=!0):g=!0;g&&d.push([k,e])}}d.sort(function(a,b){if(a[1]<b[1])return-1;if(a[1]>b[1])return 1;if(a[1]===b[1]){if(-1!==b[0].indexOf(".webp",b[0].length-5))return 1;if(-1!==a[0].indexOf(".webp",a[0].length-5))return-1}return 0});c=d}else c=[];else c=[];k=a.offsetWidth*("undefined"!==typeof window.devicePixelRatio?window.devicePixelRatio:1);d=null;h=c.length;for(f=0;f<h;f++)if(e=c[f],e[1]>=k){d=e;break}null===d&&(d=[b.getAttribute("src"),
999999]);"undefined"===typeof a.responsivelyLazyLastSetOption&&(a.responsivelyLazyLastSetOption=["",0]);a.responsivelyLazyLastSetOption[1]<d[1]&&(a.responsivelyLazyLastSetOption=d,h=d[0],"undefined"===typeof a.responsivelyLazyEventsAttached&&(a.responsivelyLazyEventsAttached=!0,b.addEventListener("load",function(){var b=a.getAttribute("data-onlazyload");null!==b&&(new Function(b)).bind(a)()},!1),b.addEventListener("error",function(){a.responsivelyLazyLastSetOption=["",0]},!1)),h===b.getAttribute("src")?
b.removeAttribute("srcset"):b.setAttribute("srcset",h))},y=function(a){if(-1===u.indexOf(a)&&v(a)){var b=a.getAttribute("data-lazycontent");null!==b?(u.push(a),q=!0,a.innerHTML=b,a=a.querySelectorAll("script"),0<a.length&&w(a,0),q=!1):t&&("img"===a.tagName.toLowerCase()?x(a,a):(b=a.querySelector("img"),null!==b&&x(a,b)))}},z=function(){for(var a=document.querySelectorAll(".responsively-lazy"),b=a.length,c=0;c<b;c++)y(a[c])};if("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;t="srcset"in document.createElement("img");var a=window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||function(a){window.setTimeout(a,1E3/60)},b=!0,c=function(){b&&(b=!1,z());a.call(null,c)};c();if(m){var d=function(){for(var a=document.querySelectorAll(".responsively-lazy"),
b=a.length,c=0;c<b;c++){var e=a[c];"undefined"===typeof e.responsivelyLazyObserverAttached&&(e.responsivelyLazyObserverAttached=!0,h.observe(e))}};var h=new IntersectionObserver(function(a){for(var b in a){var c=a[b];0<c.intersectionRatio&&y(c.target)}});var f=null}var e=function(){m?(window.clearTimeout(f),f=window.setTimeout(function(){b=!0},300)):b=!0},g=function(){for(var a=document.querySelectorAll(".responsively-lazy"),b=a.length,c=0;c<b;c++)for(var d=a[c].parentNode;d&&"html"!==d.tagName.toLowerCase();)"undefined"===
typeof d.responsivelyLazyScrollAttached&&(d.responsivelyLazyScrollAttached=!0,d.addEventListener("scroll",e)),d=d.parentNode},k=function(){window.addEventListener("resize",function(){l=window.innerWidth;p=window.innerHeight;e()});window.addEventListener("scroll",e);window.addEventListener("load",e);m&&d();g();"undefined"!==typeof MutationObserver&&(new MutationObserver(function(){q||(m&&d(),g(),e())})).observe(document.querySelector("body"),{childList:!0,subtree:!0})};"loading"===document.readyState?
document.addEventListener("DOMContentLoaded",k):k()}}return{run:z,isVisible:v}}();