forked from verlok/vanilla-lazyload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lazyload.min.js
1 lines (1 loc) · 8.48 KB
/
lazyload.min.js
1
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(n="undefined"!=typeof globalThis?globalThis:n||self).LazyLoad=t()}(this,(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 o in e)Object.prototype.hasOwnProperty.call(e,o)&&(n[o]=e[o])}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),o=t&&window.devicePixelRatio>1,i={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({},i,t)},r=function(n,t){var e,o="LazyLoad::Initialized",i=new n(t);try{e=new CustomEvent(o,{detail:{instance:i}})}catch(n){(e=document.createEvent("CustomEvent")).initCustomEvent(o,!1,!1,{instance:i})}window.dispatchEvent(e)},c="src",u="srcset",l="sizes",s="poster",d="llOriginalAttrs",f="data",_="loading",g="loaded",v="applied",b="error",p="native",h="data-",m="ll-status",E=function(n,t){return n.getAttribute(h+t)},I=function(n){return E(n,m)},y=function(n,t){return function(n,t,e){var o="data-ll-status";null!==e?n.setAttribute(o,e):n.removeAttribute(o)}(n,0,t)},k=function(n){return y(n,null)},A=function(n){return null===I(n)},L=function(n){return I(n)===p},w=[_,g,v,b],O=function(n,t,e,o){n&&"function"==typeof n&&(void 0===o?void 0===e?n(t):n(t,e):n(t,e,o))},x=function(n,e){t&&""!==e&&n.classList.add(e)},C=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)},T=function(n,t){n&&(n.toLoadCount=t)},R=function(n){for(var t,e=[],o=0;t=n.children[o];o+=1)"SOURCE"===t.tagName&&e.push(t);return e},G=function(n,t){var e=n.parentNode;e&&"PICTURE"===e.tagName&&R(e).forEach(t)},j=function(n,t){R(n).forEach(t)},D=[c],H=[c,s],V=[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){x(n,t.class_applied),y(n,v),e&&(t.unobserve_completed&&z(n,t),O(t.callback_applied,n,e))},K=function(n,t,e){x(n,t.class_loading),y(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,V),W(n,t)})),S(n,V),W(n,t)},IFRAME:function(n,t){S(n,D),Q(n,c,E(n,t.data_src))},VIDEO:function(n,t){j(n,(function(n){S(n,D),Q(n,c,E(n,t.data_src))})),S(n,H),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 o=t[e];nn(n,e,o)}delete n.llEvLisnrs}},on=function(n,t,e){!function(n){delete n.llTempImage}(n),N(e,-1),function(n){n&&(n.toLoadCount-=1)}(e),C(n,t.class_loading),t.unobserve_completed&&z(n,e)},an=function(n,t,e){var o=M(n)||n;tn(o)||function(n,t,e){tn(n)||(n.llEvLisnrs={});var o="VIDEO"===n.tagName?"loadeddata":"load";$(n,o,t),$(n,"error",e)}(o,(function(i){!function(n,t,e,o){var i=L(t);on(t,e,o),x(t,e.class_loaded),y(t,g),O(e.callback_loaded,t,o),i||Z(e,o)}(0,n,t,e),en(o)}),(function(i){!function(n,t,e,o){var i=L(t);on(t,e,o),x(t,e.class_error),y(t,b),O(e.callback_error,t,o),e.restore_on_error&&U(t,V),i||Z(e,o)}(0,n,t,e),en(o)}))},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 i=E(n,t.data_bg),a=E(n,t.data_bg_hidpi),r=o&&a?a:i;r&&(n.style.backgroundImage='url("'.concat(r,'")'),M(n).setAttribute(c,r),K(n,t,e))}(n,t,e),function(n,t,e){var i=E(n,t.data_bg_multi),a=E(n,t.data_bg_multi_hidpi),r=o&&a?a:i;r&&(n.style.backgroundImage=r,q(n,t,e))}(n,t,e),function(n,t,e){var o=E(n,t.data_bg_set);if(o){var i=o.split("|"),a=i.map((function(n){return"image-set(".concat(n,")")}));n.style.backgroundImage=a.join(),""===n.style.backgroundImage&&(a=i.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 o=X[n.tagName];o&&(o(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,V)})),U(n,V)},ln={IMG:un,IFRAME:function(n){U(n,D)},VIDEO:function(n){j(n,(function(n){U(n,D)})),U(n,H),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)||(C(n,t.class_entered),C(n,t.class_exited),C(n,t.class_applied),C(n,t.class_loading),C(n,t.class_loaded),C(n,t.class_error))}(n,t),k(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,o){var i=function(n){return w.indexOf(I(n))>=0}(n);y(n,"entered"),x(n,e.class_entered),C(n,e.class_exited),function(n,t,e){t.unobserve_entered&&z(n,e)}(n,e,o),O(e.callback_enter,n,t,o),i||rn(n,e,o)}(n.target,n,t,e):function(n,t,e,o){A(n)||(x(n,e.class_exited),function(n,t,e,o){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),C(n,e.class_loading),N(o,-1),k(n),O(e.callback_cancel,n,t,o))}(n,t,e,o),O(e.callback_exit,n,t,o))}(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)},pn=function(n,t){return function(n){return gn(n).filter(A)}(n||vn(t))},hn=function(n,e){var o=a(n);this._settings=o,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)))}(o,this),function(n,e){t&&(e._onlineHandler=function(){!function(n,t){var e;(e=vn(n),gn(e).filter(bn)).forEach((function(t){C(t,n.class_error),k(t)})),t.update()}(n,e)},window.addEventListener("online",e._onlineHandler))}(o,this),this.update(e)};return hn.prototype={update:function(n){var t,o,i=this._settings,a=pn(n,i);T(this,a.length),e?this.loadAll(a):fn(i)?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),y(n,p)}(n,t,e)})),T(e,0)}(a,i,this):(o=a,function(n){n.disconnect()}(t=this._observer),function(n,t){t.forEach((function(t){n.observe(t)}))}(t,o))},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;pn(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)}))}},hn.load=function(n,t){var e=a(t);rn(n,e)},hn.resetStatus=function(n){k(n)},t&&function(n,t){if(t)if(t.length)for(var e,o=0;e=t[o];o+=1)r(n,e);else r(n,t)}(hn,window.lazyLoadOptions),hn}));