forked from verlok/vanilla-lazyload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lazyload.iife.min.js
1 lines (1 loc) · 8.29 KB
/
lazyload.iife.min.js
1
var LazyLoad=function(){"use strict";function n(){return n=Object.assign||function(n){for(var t=1;t<arguments.length;t++){var e=arguments[t];for(var i in e)Object.prototype.hasOwnProperty.call(e,i)&&(n[i]=e[i])}return n},n.apply(this,arguments)}var t="undefined"!=typeof window,e=t&&!("onscroll"in window)||"undefined"!=typeof navigator&&/(gle|ing|ro)bot|crawl|spider/i.test(navigator.userAgent),i=t&&window.devicePixelRatio>1,o={elements_selector:".lazy",container:e||t?document:null,threshold:300,thresholds:null,data_src:"src",data_srcset:"srcset",data_sizes:"sizes",data_bg:"bg",data_bg_hidpi:"bg-hidpi",data_bg_multi:"bg-multi",data_bg_multi_hidpi:"bg-multi-hidpi",data_bg_set:"bg-set",data_poster:"poster",class_applied:"applied",class_loading:"loading",class_loaded:"loaded",class_error:"error",class_entered:"entered",class_exited:"exited",unobserve_completed:!0,unobserve_entered:!1,cancel_on_exit:!0,callback_enter:null,callback_exit:null,callback_applied:null,callback_loading:null,callback_loaded:null,callback_error:null,callback_finish:null,callback_cancel:null,use_native:!1,restore_on_error:!1},a=function(t){return n({},o,t)},r=function(n,t){var e,i="LazyLoad::Initialized",o=new n(t);try{e=new CustomEvent(i,{detail:{instance:o}})}catch(n){(e=document.createEvent("CustomEvent")).initCustomEvent(i,!1,!1,{instance:o})}window.dispatchEvent(e)},c="src",u="srcset",l="sizes",s="poster",d="llOriginalAttrs",f="data",_="loading",g="loaded",v="applied",b="error",h="native",m="data-",p="ll-status",E=function(n,t){return n.getAttribute(m+t)},I=function(n){return E(n,p)},k=function(n,t){return function(n,t,e){var i="data-ll-status";null!==e?n.setAttribute(i,e):n.removeAttribute(i)}(n,0,t)},y=function(n){return k(n,null)},A=function(n){return null===I(n)},L=function(n){return I(n)===h},w=[_,g,v,b],O=function(n,t,e,i){n&&"function"==typeof n&&(void 0===i?void 0===e?n(t):n(t,e):n(t,e,i))},C=function(n,e){t&&""!==e&&n.classList.add(e)},x=function(n,e){t&&""!==e&&n.classList.remove(e)},M=function(n){return n.llTempImage},z=function(n,t){if(t){var e=t._observer;e&&e.unobserve(n)}},N=function(n,t){n&&(n.loadingCount+=t)},R=function(n,t){n&&(n.toLoadCount=t)},T=function(n){for(var t,e=[],i=0;t=n.children[i];i+=1)"SOURCE"===t.tagName&&e.push(t);return e},G=function(n,t){var e=n.parentNode;e&&"PICTURE"===e.tagName&&T(e).forEach(t)},D=function(n,t){T(n).forEach(t)},H=[c],V=[c,s],j=[c,u,l],F=[f],B=function(n){return!!n[d]},J=function(n){return n[d]},P=function(n){return delete n[d]},S=function(n,t){if(!B(n)){var e={};t.forEach((function(t){e[t]=n.getAttribute(t)})),n[d]=e}},U=function(n,t){if(B(n)){var e=J(n);t.forEach((function(t){!function(n,t,e){e?n.setAttribute(t,e):n.removeAttribute(t)}(n,t,e[t])}))}},q=function(n,t,e){C(n,t.class_applied),k(n,v),e&&(t.unobserve_completed&&z(n,t),O(t.callback_applied,n,e))},K=function(n,t,e){C(n,t.class_loading),k(n,_),e&&(N(e,1),O(t.callback_loading,n,e))},Q=function(n,t,e){e&&n.setAttribute(t,e)},W=function(n,t){Q(n,l,E(n,t.data_sizes)),Q(n,u,E(n,t.data_srcset)),Q(n,c,E(n,t.data_src))},X={IMG:function(n,t){G(n,(function(n){S(n,j),W(n,t)})),S(n,j),W(n,t)},IFRAME:function(n,t){S(n,H),Q(n,c,E(n,t.data_src))},VIDEO:function(n,t){D(n,(function(n){S(n,H),Q(n,c,E(n,t.data_src))})),S(n,V),Q(n,s,E(n,t.data_poster)),Q(n,c,E(n,t.data_src)),n.load()},OBJECT:function(n,t){S(n,F),Q(n,f,E(n,t.data_src))}},Y=["IMG","IFRAME","VIDEO","OBJECT"],Z=function(n,t){!t||function(n){return n.loadingCount>0}(t)||function(n){return n.toLoadCount>0}(t)||O(n.callback_finish,t)},$=function(n,t,e){n.addEventListener(t,e),n.llEvLisnrs[t]=e},nn=function(n,t,e){n.removeEventListener(t,e)},tn=function(n){return!!n.llEvLisnrs},en=function(n){if(tn(n)){var t=n.llEvLisnrs;for(var e in t){var i=t[e];nn(n,e,i)}delete n.llEvLisnrs}},on=function(n,t,e){!function(n){delete n.llTempImage}(n),N(e,-1),function(n){n&&(n.toLoadCount-=1)}(e),x(n,t.class_loading),t.unobserve_completed&&z(n,e)},an=function(n,t,e){var i=M(n)||n;tn(i)||function(n,t,e){tn(n)||(n.llEvLisnrs={});var i="VIDEO"===n.tagName?"loadeddata":"load";$(n,i,t),$(n,"error",e)}(i,(function(o){!function(n,t,e,i){var o=L(t);on(t,e,i),C(t,e.class_loaded),k(t,g),O(e.callback_loaded,t,i),o||Z(e,i)}(0,n,t,e),en(i)}),(function(o){!function(n,t,e,i){var o=L(t);on(t,e,i),C(t,e.class_error),k(t,b),O(e.callback_error,t,i),e.restore_on_error&&U(t,j),o||Z(e,i)}(0,n,t,e),en(i)}))},rn=function(n,t,e){!function(n){return Y.indexOf(n.tagName)>-1}(n)?function(n,t,e){!function(n){n.llTempImage=document.createElement("IMG")}(n),an(n,t,e),function(n){B(n)||(n[d]={backgroundImage:n.style.backgroundImage})}(n),function(n,t,e){var o=E(n,t.data_bg),a=E(n,t.data_bg_hidpi),r=i&&a?a:o;r&&(n.style.backgroundImage='url("'.concat(r,'")'),M(n).setAttribute(c,r),K(n,t,e))}(n,t,e),function(n,t,e){var o=E(n,t.data_bg_multi),a=E(n,t.data_bg_multi_hidpi),r=i&&a?a:o;r&&(n.style.backgroundImage=r,q(n,t,e))}(n,t,e),function(n,t,e){var i=E(n,t.data_bg_set);if(i){var o=i.split("|"),a=o.map((function(n){return"image-set(".concat(n,")")}));n.style.backgroundImage=a.join(),""===n.style.backgroundImage&&(a=o.map((function(n){return"-webkit-image-set(".concat(n,")")})),n.style.backgroundImage=a.join()),q(n,t,e)}}(n,t,e)}(n,t,e):function(n,t,e){an(n,t,e),function(n,t,e){var i=X[n.tagName];i&&(i(n,t),K(n,t,e))}(n,t,e)}(n,t,e)},cn=function(n){n.removeAttribute(c),n.removeAttribute(u),n.removeAttribute(l)},un=function(n){G(n,(function(n){U(n,j)})),U(n,j)},ln={IMG:un,IFRAME:function(n){U(n,H)},VIDEO:function(n){D(n,(function(n){U(n,H)})),U(n,V),n.load()},OBJECT:function(n){U(n,F)}},sn=function(n,t){(function(n){var t=ln[n.tagName];t?t(n):function(n){if(B(n)){var t=J(n);n.style.backgroundImage=t.backgroundImage}}(n)})(n),function(n,t){A(n)||L(n)||(x(n,t.class_entered),x(n,t.class_exited),x(n,t.class_applied),x(n,t.class_loading),x(n,t.class_loaded),x(n,t.class_error))}(n,t),y(n),P(n)},dn=["IMG","IFRAME","VIDEO"],fn=function(n){return n.use_native&&"loading"in HTMLImageElement.prototype},_n=function(n,t,e){n.forEach((function(n){return function(n){return n.isIntersecting||n.intersectionRatio>0}(n)?function(n,t,e,i){var o=function(n){return w.indexOf(I(n))>=0}(n);k(n,"entered"),C(n,e.class_entered),x(n,e.class_exited),function(n,t,e){t.unobserve_entered&&z(n,e)}(n,e,i),O(e.callback_enter,n,t,i),o||rn(n,e,i)}(n.target,n,t,e):function(n,t,e,i){A(n)||(C(n,e.class_exited),function(n,t,e,i){e.cancel_on_exit&&function(n){return I(n)===_}(n)&&"IMG"===n.tagName&&(en(n),function(n){G(n,(function(n){cn(n)})),cn(n)}(n),un(n),x(n,e.class_loading),N(i,-1),y(n),O(e.callback_cancel,n,t,i))}(n,t,e,i),O(e.callback_exit,n,t,i))}(n.target,n,t,e)}))},gn=function(n){return Array.prototype.slice.call(n)},vn=function(n){return n.container.querySelectorAll(n.elements_selector)},bn=function(n){return function(n){return I(n)===b}(n)},hn=function(n,t){return function(n){return gn(n).filter(A)}(n||vn(t))},mn=function(n,e){var i=a(n);this._settings=i,this.loadingCount=0,function(n,t){fn(n)||(t._observer=new IntersectionObserver((function(e){_n(e,n,t)}),function(n){return{root:n.container===document?null:n.container,rootMargin:n.thresholds||n.threshold+"px"}}(n)))}(i,this),function(n,e){t&&(e._onlineHandler=function(){!function(n,t){var e;(e=vn(n),gn(e).filter(bn)).forEach((function(t){x(t,n.class_error),y(t)})),t.update()}(n,e)},window.addEventListener("online",e._onlineHandler))}(i,this),this.update(e)};return mn.prototype={update:function(n){var t,i,o=this._settings,a=hn(n,o);R(this,a.length),e?this.loadAll(a):fn(o)?function(n,t,e){n.forEach((function(n){-1!==dn.indexOf(n.tagName)&&function(n,t,e){n.setAttribute("loading","lazy"),an(n,t,e),function(n,t){var e=X[n.tagName];e&&e(n,t)}(n,t),k(n,h)}(n,t,e)})),R(e,0)}(a,o,this):(i=a,function(n){n.disconnect()}(t=this._observer),function(n,t){t.forEach((function(t){n.observe(t)}))}(t,i))},destroy:function(){this._observer&&this._observer.disconnect(),t&&window.removeEventListener("online",this._onlineHandler),vn(this._settings).forEach((function(n){P(n)})),delete this._observer,delete this._settings,delete this._onlineHandler,delete this.loadingCount,delete this.toLoadCount},loadAll:function(n){var t=this,e=this._settings;hn(n,e).forEach((function(n){z(n,t),rn(n,e,t)}))},restoreAll:function(){var n=this._settings;vn(n).forEach((function(t){sn(t,n)}))}},mn.load=function(n,t){var e=a(t);rn(n,e)},mn.resetStatus=function(n){y(n)},t&&function(n,t){if(t)if(t.length)for(var e,i=0;e=t[i];i+=1)r(n,e);else r(n,t)}(mn,window.lazyLoadOptions),mn}();