1 |
- !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.LazyLoadImg=e()}(this,function(){"use strict";var t=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},o=t.getBoundingClientRect(),n=t.offsetWidth,i=t.offsetHeight,r=window.innerWidth,a=window.innerHeight,s=!(o.right-e.left<=0&&o.left+n-e.left<=0||o.left+e.right>=r&&o.right+e.right>=n+r),c=!(o.bottom-e.top<=0&&o.top+i-e.top<=0||o.top+e.bottom>=a&&o.bottom+e.bottom>=i+a);return 0!==t.width&&0!==t.height&&s&&c},e=document.createElement("canvas");e.getContext("2d").globalAlpha=0;var o={},n=function(t,n,i){if(o[t])return o[t];e.width=n,e.height=i;var r=e.toDataURL("image/png");return o[t]=r,r},i=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},r=function(){function t(t,e){for(var o=0;o<e.length;o++){var n=e[o];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}return function(e,o,n){return o&&t(e.prototype,o),n&&t(e,n),e}}(),a=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var o=arguments[e];for(var n in o)Object.prototype.hasOwnProperty.call(o,n)&&(t[n]=o[n])}return t},s=window;return function(){function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};i(this,e),this.options={el:document.querySelector("body"),mode:"default",time:300,done:!0,diy:{backgroundSize:"cover",backgroundRepeat:"no-repeat",backgroundPosition:"center center"},position:{top:0,right:0,bottom:0,left:0},before:function(t){},success:function(t){},error:function(t){}},t.position=a({},this.options.position,t.position),t.diy=a({},this.options.diy,t.diy),a(this.options,t),this.start()}return r(e,[{key:"start",value:function(){this._timer=!0,this._start()}},{key:"_start",value:function(){var e=this,o=this.options;clearTimeout(this._timer),this._timer&&(this._timer=setTimeout(function(){var n=Array.prototype.slice.apply(o.el.querySelectorAll("[data-src]"));if(!n.length&&o.done)return clearTimeout(e._timer);n.forEach(function(n){!n.dataset.LazyLoadImgState&&t(n,o.position)&&e.loadImg(n)}),e._start()},o.time))}},{key:"loadImg",value:function(t){var e=this,o=this.options;t.dataset.LazyLoadImgState="start",o.before.call(this,t);var i=new s.Image;i.src=t.dataset.src,i.addEventListener("load",function(){return"diy"===o.mode?(t.src=n(t.src,t.width,t.height),o.diy.backgroundImage="url("+i.src+")",a(t.style,o.diy)):t.src=i.src,delete t.dataset.src,t.dataset.LazyLoadImgState="success",o.success.call(e,t)},!1),i.addEventListener("error",function(){delete t.dataset.src,t.dataset.LazyLoadImgState="error",o.error.call(e,t)},!1)}},{key:"destroy",value:function(){delete this._timer}}]),e}()});
|