image-lazyload
Version:
Loaded images of lazy loading components. Compatible with IE7 + browser.
2 lines • 4.46 kB
JavaScript
/*! Project:lazyload, Create:单炒饭 2018.01.23 19:40, Update:单炒饭 2018.02.02 15:35 */
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):"function"==typeof define&&(define.cmd||define.hjs)?define(function(require,exports,e){e.exports=t()}):e.Lazyload=t()}(this,function(){"use strict";var e=function(e,t){if(window.Blob)return new Blob(e,t);if(window.BlobBuilder=window.BlobBuilder||window.WebKitBlobBuilder||window.MozBlobBuilder||window.MSBlobBuilder,window.BlobBuilder){var o=new BlobBuilder;return o.append(e),o.getBlob("object"==typeof t?t.type:undefined)}return o},t=function(e,t,o,n){e.addEventListener?e.addEventListener(t,o,!1):e.attachEvent&&e.attachEvent("on"+t,function(){o.call(e,window.event)})},o=window.URL||window.webkitURL||window.mozURL||window.msURL;return function(){function n(e){var t=this,o=t.config={},n=t.data={};t.obj={doc:document.documentElement,body:document.body};for(var i in e)t.config[i]=e[i];t.config.range="numver"==typeof t.config.range?t.config.range:100,t.imageExtend();for(var i=0,a=o.obj.length;i<a;i++){var r=o.obj[i],d=r.lazy_dataSrc=r.getAttribute("data-src");r.lazy_dataCover=r.getAttribute("data-cover");""===r.src&&(r.src="data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg=="),r.removeAttribute("data-src"),n[d]===undefined&&(n[d]=[]),n[d].push(r)}}return n.prototype.init=function(){var e,o,n=this;n.config,n.data,n.obj;(e=function(){for(var e=n.getShowList(),t=0,o=e.length;t<o;t++){var i=e[t];n.loadImg(i)}})();var i=function(){clearTimeout(o),o=setTimeout(e,200)};t(window,"scroll",i),t(window,"resize",i)},n.prototype.imageExtend=function(){var t=this;Image.prototype.load=function(n,i){var a=function(e){for(var o=0,n=i.length;o<n;o++){var a=i[o];t.updateImg(a,e)}},r=function(e){for(var o=0,n=i.length;o<n;o++){var a=i[o];a.completedPercentage=e,e.o=a,t.tips(e)}};if(e&&o&&XMLHttpRequest){var d=new XMLHttpRequest;d.open("GET",n,!0),d.responseType="arraybuffer",d.onload=function(t){var n=d.getAllResponseHeaders(),i=n.match(/^Content-Type\:\s*(.*?)$/im),r=i[1]||"image/png",l=e([this.response],{type:r}),c=o.createObjectURL(l);a(c)},d.onloadstart=d.onprogress=d.onloadend=function(e){var t={loaded:e.loaded,total:e.total,status:e.type,schedule:0===e.loaded?0:e.loaded/e.total};r(t)},d.send()}else{var l,c={loaded:undefined,total:undefined,status:"loaded",schedule:0};l=setInterval(function(){c.schedule>.98&&clearInterval(l),c.schedule=c.schedule+.01,c.status="progress",r(c)},100),this.onload=function(){clearInterval(l),a(this.src),c.status="loaded",c.schedule=1,r(c)}}}},n.prototype.getWinSize=function(){var e=this,t=e.obj,t={w:window.innerWidth||t.doc.clientWidth,h:window.innerHeight||t.doc.clientHeight};return t},n.prototype.getWinScroll=function(){return{x:window.scrollX||document.documentElement.scrollLeft||window.pageXOffset||document.body.scrollLeft,y:window.scrollY||document.documentElement.scrollTop||window.pageYOffset||document.body.scrollTop}},n.prototype.loadImg=function(e){var t=this,o=t.data;if(!e.lazy_isInit){var n=o[e.lazy_dataSrc],i=new Image;i.load(e.lazy_dataSrc,n),i.src=e.lazy_dataSrc;for(var a=0,r=n.length;a<r;a++){var d=n[a];d.lazy_isInit||(d.lazy_isInit=!0,d.lazy_dataCover&&t.updateImg(d,d.lazy_dataCover))}}},n.prototype.updateImg=function(e,t){"IMG"===e.tagName?e.src=t:e.style.backgroundImage="url("+t+")"},n.prototype.getElementTop=function(e){for(var t=e.offsetTop,o=e.offsetParent;null!==o;)t+=o.offsetTop,o=o.offsetParent;return t},n.prototype.getElementLeft=function(e){for(var t=e.offsetLeft,o=e.offsetParent;null!==o;)t+=o.offsetLeft,o=o.offsetParent;return t},n.prototype.getShowList=function(){for(var e=this,t=e.config,o=e.getWinScroll(),n=e.getWinSize(),i=[],a=0,r=t.obj.length;a<r;a++){var d=t.obj[a],l=e.getElementLeft(d),c=e.getElementTop(d),f=d.clientWidth,s=d.clientHeight,u=l+f-o.x>0-t.range,p=n.w+o.x+t.range>l,g=c+s-o.y>0-t.range,y=c<o.y+n.h+t.range,w=d.lazy_isInit;u&&p&&g&&y&&!w&&i.push(d)}return i},n.prototype.tips=function(e){var t=parseInt(100*e.schedule)+"%",o=e.o;o.lazy_isEchoTip===undefined&&(o.lazy_isEchoTip=!0,o.lazy_oTip=document.createElement("span"),o.lazy_oTip.className="lazy_tip",o.parentNode.insertBefore(o.lazy_oTip,o.nextSibling)),o.lazy_oTip.innerHTML=t,1===e.schedule&&o.lazy_isEchoTip&&(o.parentNode.removeChild(o.lazy_oTip),o.lazy_isEchoTip=undefined)},n}()});