forked from aFarkas/lazysizes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lazysizes.min.js
3 lines (3 loc) · 5.67 KB
/
lazysizes.min.js
1
2
3
/*! lazysizes - v1.0.0-pre - 2015-02-23
Licensed MIT */
!function(a,b){a.lazySizes=b(a,a.document),"function"==typeof define&&define.amd&&define(a.lazySizes)}(window,function(a,b){"use strict";if(b.getElementsByClassName){var c,d=b.documentElement,e=/^picture$/i,f=["load","error","lazyincluded","_lazyloaded"],g=function(a,b){var c=new RegExp("(\\s|^)"+b+"(\\s|$)");return a.className.match(c)&&c},h=function(a,b){g(a,b)||(a.className+=" "+b)},i=function(a,b){var c;(c=g(a,b))&&(a.className=a.className.replace(c," "))},j=function(a,b,c){var d=c?"addEventListener":"removeEventListener";c&&j(a,b),f.forEach(function(c){a[d](c,b)})},k=function(a,c,d,e,f){var g=b.createEvent("Event");return g.initEvent(c,!e,!f),g.details=d||{},a.dispatchEvent(g),g},l=function(b,d){var e;a.HTMLPictureElement||((e=a.picturefill||a.respimage||c.polyfill)?e({reevaluate:!0,reparse:!0,elements:[b]}):d&&d.src&&(b.src=d.src))},m=function(a,b){return getComputedStyle(a,null)[b]},n=function(a,b){for(var d=a.offsetWidth;d<c.minSize&&b&&!a._lazysizesWidth;)d=b.offsetWidth,b=b.parentNode;return d},o=function(a){var c,d,e=function(){c&&(c=!1,a())},f=function(){clearInterval(d),b.hidden||(e(),d=setInterval(e,51))};return b.addEventListener("visibilitychange",f),f(),function(a){c=!0,a===!0&&e()}},p=function(){var f,n,r,s,t,u,v,w,x,y,z,A=a.HTMLPictureElement&&navigator.userAgent.match(/hrome\/(\d+)/)&&40==RegExp.$1,B=/^img$/i,C=/^iframe$/i,D="onscroll"in a,E=-2,F=E,G=E,H=E,I=0,J=0,K=0,L=function(a){J--,a&&a.target&&j(a.target,L),(!a||0>J||!a.target)&&(J=0)},M=function(a,b){var c,d=a,e="hidden"!=m(a,"visibility");for(w-=b,z+=b,x-=b,y+=b;e&&(d=d.offsetParent);)e=s&&2>J||(m(d,"opacity")||1)>0,e&&"visible"!=m(d,"overflow")&&(c=d.getBoundingClientRect(),e=y>c.left-1&&x<c.right+1&&z>c.top-1&&w<c.bottom+1);return e},N=function(){var a,b,d,e,g,h,i,j,k,l=n.length;if(l&&p.m){for(b=Date.now(),f||S(),a=K;l>a;a++,K++)if(n[a]&&!n[a]._lazyRace)if(D){if((j=n[a].getAttribute("data-expand"))&&(h=1*j)||(h=H),!(J>6&&(!j||"src"in n[a])))if(h>E&&(p.m<2||J>3)&&(h=E),k!==h&&(u=innerWidth+h,v=innerHeight+h,i=-1*h,k=h),d=n[a].getBoundingClientRect(),(z=d.bottom)>=i&&(w=d.top)<=v&&(y=d.right)>=i&&(x=d.left)<=u&&(z||y||x||w)&&(s&&G>H&&3>J&&4>I&&!j||M(n[a],h)))K--,b+=2,R(n[a]),g=!0;else{if(Date.now()-b>3)return K++,void O();!g&&s&&!e&&3>J&&4>I&&(r[0]||c.preloadAfterLoad)&&(r[0]||!j&&(z||y||x||w||"auto"!=n[a].getAttribute(c.sizesAttr)))&&(e=r[0]||n[a])}}else R(n[a]);K=0,I++,G>H&&2>J&&I>5&&p.m>2?(H=G,I=0,O()):H=H!=F&&p.m>1&&I>4?F:E,e&&!g&&R(e)}},O=o(N),P=function(a){h(a.target,c.loadedClass),i(a.target,c.loadingClass),j(a.target,P)},Q=function(a,b){try{a.contentWindow.location.replace(b)}catch(c){a.setAttribute("src",b)}},R=function(a,b){var d,f,m,n,o,p,r,u,v,w,x,y=a.currentSrc||a.src,z=B.test(a.nodeName),D=a.getAttribute(c.sizesAttr)||a.getAttribute("sizes"),E="auto"==D;if(!E&&s||!z||!y||a.complete||g(a,c.errorClass)){if(a._lazyRace=!0,!(v=k(a,"lazybeforeunveil",{force:!!b})).defaultPrevented){if(D&&(E?q.updateElem(a,!0):a.setAttribute("sizes",D)),p=a.getAttribute(c.srcsetAttr),o=a.getAttribute(c.srcAttr),z&&(r=a.parentNode,u=e.test(r.nodeName||"")),w=v.details.firesLoad||"src"in a&&(p||o||u),w&&(J++,j(a,L,!0),clearTimeout(t),t=setTimeout(L,3e3)),u)for(d=r.getElementsByTagName("source"),f=0,m=d.length;m>f;f++)(x=c.customMedia[d[f].getAttribute("data-media")||d[f].getAttribute("media")])&&d[f].setAttribute("media",x),n=d[f].getAttribute(c.srcsetAttr),n&&d[f].setAttribute("srcset",n);p?(a.setAttribute("srcset",p),A&&D&&a.removeAttribute("src")):o&&(C.test(a.nodeName)?Q(a,o):a.setAttribute("src",o)),h(a,c.loadingClass),j(a,P,!0)}setTimeout(function(){a._lazyRace&&delete a._lazyRace,"auto"==D&&h(a,c.autosizesClass),(p||u)&&l(a,{src:o}),i(a,c.lazyClass),(!w||a.complete&&y==(a.currentSrc||a.src))&&(w&&L(v),P(v)),a=null})}},S=function(){f||(F=Math.max(Math.min(c.expand||140,300),9),G=4*F),f=!0},T=function(){p.m=3},U=function(){s=!0,I+=6,T(),O(!0)};return{_i:function(){n=b.getElementsByClassName(c.lazyClass),r=b.getElementsByClassName(c.lazyClass+" "+c.preloadClass),c.scroll&&addEventListener("scroll",O,!0),addEventListener("resize",function(){f=!1,O()},!0),a.MutationObserver?new MutationObserver(O).observe(d,{childList:!0,subtree:!0,attributes:!0}):(d.addEventListener("DOMNodeInserted",O,!0),d.addEventListener("DOMAttrModified",O,!0),setInterval(O,3e3)),addEventListener("hashchange",O,!0),["transitionstart","transitionend","load","focus","mouseover","animationend","click"].forEach(function(a){b.addEventListener(a,O,!0)}),(s=/d$|^c/.test(b.readyState))||(addEventListener("load",U),b.addEventListener("DOMContentLoaded",O)),setTimeout(T,777),O(n.length>0)},m:1,checkElems:O,unveil:R}}(),q=function(){var a,d=function(a,b){var c,d,f,g,h,i=a.parentNode;if(i&&(c=n(a,i),h=k(a,"lazybeforesizes",{width:c,dataAttr:!!b}),!h.defaultPrevented&&(c=h.details.width,c&&c!==a._lazysizesWidth))){if(a._lazysizesWidth=c,c+="px",a.setAttribute("sizes",c),e.test(i.nodeName||""))for(d=i.getElementsByTagName("source"),f=0,g=d.length;g>f;f++)d[f].setAttribute("sizes",c);h.details.dataAttr||l(a,h.details)}},f=function(){var b,c=a.length;if(c)for(b=0;c>b;b++)d(a[b])},g=o(f);return{_i:function(){a=b.getElementsByClassName(c.autosizesClass),addEventListener("resize",g)},checkElems:g,updateElem:d}}(),r=function(){r.i||(r.i=!0,q._i(),p._i())};return function(){var b,d={lazyClass:"lazyload",loadedClass:"lazyloaded",loadingClass:"lazyloading",preloadClass:"lazypreload",errorClass:"lazyerror",scroll:!0,autosizesClass:"lazyautosizes",srcAttr:"data-src",srcsetAttr:"data-srcset",sizesAttr:"data-sizes",minSize:50,customMedia:{},init:!0};c=a.lazySizesConfig||{};for(b in d)b in c||(c[b]=d[b]);a.lazySizesConfig=c,setTimeout(function(){c.init&&r()})}(),{cfg:c,autoSizer:q,loader:p,init:r,uP:l,aC:h,rC:i,hC:g,fire:k,gW:n}}});