-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmob.min.js
1 lines (1 loc) · 5.98 KB
/
mob.min.js
1
window.matchMedia=window.matchMedia||function(a){var c,d=a.documentElement,e=d.firstElementChild||d.firstChild,f=a.createElement("body"),g=a.createElement("div");return g.id="mq-test-1",g.style.cssText="position:absolute;top:-100em",f.style.background="none",f.appendChild(g),function(a){return g.innerHTML='­<style media="'+a+'"> #mq-test-1 { width: 42px; }</style>',d.insertBefore(f,e),c=42==g.offsetWidth,d.removeChild(f),{matches:c,media:a}}}(document),function(){if(!window.matchMedia("").addListener){var a=window.matchMedia;window.matchMedia=function(b){var f,c=a(b),d=[],e=!1,g=function(){var f=a(b),g=f.matches&&!e,h=!f.matches&&e;if(g||h)for(var i=0,j=d.length;j>i;i++)d[i].call(c,f);e=f.matches};return c.addListener=function(a){d.push(a),f||(f=setInterval(g,1e3))},c.removeListener=function(a){for(var b=0,c=d.length;c>b;b++)d[b]===a&&d.splice(b,1);!d.length&&f&&clearInterval(f)},c}}}(),void 0===window.console&&(console={log:function(){}});var mob={}||mob;(function(){function c(a,b,c){var d=window.getComputedStyle(a)[b];return d=c===!0?+parseFloat(d)||0:d}function e(a){return a?!1:!0}function f(a,b){a.style.webkitTransition=b,a.style.MozTransition=b,a.style.msTransition=b,a.style.OTransition=b,a.style.transition=b}var a=document;a.styleSheets,mob={name:"mob.js",version:"0.6",defaults:{requestType:"%fx%w.%e",autoStart:!0,autoReload:!0,retinaSupport:!1,offsetHeight:100,offsetWidth:100,minHeight:200,minWidth:200,lazyLoad:!0,alterImg:"data:image/gif;base64,R0lGODlhEAAKAIAAAAAAAP///yH/C1hNUCBEYXRhWE1QPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0wTXBDZWhpSHpyZVN6TlRjemtjOWQiPz4gPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iQWRvYmUgWE1QIENvcmUgNS4zLWMwMTEgNjYuMTQ1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XRzcnFwb25tbGtqaWhnZmVkY2JhYF9eXVxbWllYV1ZVVFNSUVBPTk1MS0pJSEdGRURDQkFAPz49PDs6OTg3NjU0MzIxMC8uLSwrKikoJyYlJCMiISAfHh0cGxoZGBcWFRQTEhEQDw4NDAsKCQgHBgUEAwIBAAAh+QQAAAAAACwAAAAAEAAKAAACC4SPqcvtD6OctK4CADs="},scrollTimer:!1,__preventEvent:!0,__viewportHeight:0,__viewportWidth:0,__basket:[],__book:[],setConfig:function(a){var b=this.config;for(var c in a)b[c]=void 0===b[c]?a[c]:b[c];return b.DPR=b.retinaSupport&&window.devicePixelRatio>1?window.devicePixelRatio:1,b},touch:function(){this.config=this.setConfig(this.defaults),this.config.autoStart&&this.init()},init:function(){var a=this.config;a.lazyLoad&&window.addEventListener("scroll",function(){mob.setShowBooks()}),a.autoReload&&this.addMQListener(),this.refresh()},refresh:function(){this.__basket=[],this.register(a)},register:function(a){for(var b=a.getElementsByTagName("img"),d=b.length;d--;){var e=b[d].getAttribute("data-src"),f=b[d].src;if(null!==e){b[d]._file=e.split(/(?:\.([^.]+))?$/);var g=c(b[d],"minHeight"),h=c(b[d],"minWidth");b[d].style.minHeight=1>parseFloat(g)?this.config.minHeight+"px":g,b[d].style.minWidth=1>parseFloat(h)?this.config.minWidth+"px":h,b[d].src=""===f?this.config.alterImg:f,b[d].style.opacity=.2,this.__basket.push(b[d])}}this.addBooks(!0)},resetBooks:function(){this.__book=[],this.__preventEvent=e(this.__preventEvent),this.addBooks(this.__preventEvent)},addBooks:function(a){if(a){for(var b=this.__basket.length;b--;){var d=this.__basket[b],e={};e._width=c(d,"width",!0),e._height=c(d,"height",!0),e._display=c(d,"display"),(void 0===d._width||void 0===d._height||e._width>d._width||e._height>d._height)&&"none"!==e._display&&(d.position=d.getBoundingClientRect(),d._width=e._width,d._height=e._height,this.__book.push(d))}this.showBooks()}},showBooks:function(){function c(a){return Math.min(document.body[a],document.documentElement[a])}var a="showElement";this.config.lazyLoad&&(this.__viewportHeight=c("clientHeight"),this.__viewportWidth=c("clientWidth"),a="checkPosition",this.bodyRect=document.body.getBoundingClientRect());for(var b=0;this.__book.length-1>=b;b++)void 0!==this.__book[b]&&this[a](this.__book[b])},checkPosition:function(a){var b=a.position,c=this.bodyRect;f(a,"opacity ease 0.6s"),b.top+c.top<=this.__viewportHeight&&b.left+c.left<=this.__viewportWidth&&this.showElement(a)},showElement:function(a){var b=this.config.requestType,c=a._file,d=Math.ceil(this.config.DPR*a._width),e=Math.ceil(this.config.DPR*a._height),f=b.replace("%f",c[0]).replace("%w",d).replace("%h",e).replace("%e",c[1]);a.src!==f&&(a.src=f,a.addEventListener("load",function(){mob.setAttribute(this)}),a.addEventListener("error",function(b){console.log(b.status+"Can't load - "+this._file[0]+"."+this._file[1]);var c=mob.__book.indexOf(a);c>=0&&mob.__book.splice(c,1)}))},setAttribute:function(a){a.style.opacity=1,this.__basket.indexOf(a);var c=this.__book.indexOf(a);c>=0&&this.__book.splice(c,1)},addMQListener:function(){for(var a=document.styleSheets,b=a.length;b--;)for(var c=a[b].cssRules,d=c.length,e=d;e--;)if(void 0!==c[e].media){var f=c[e].media.mediaText;eventObject=this["mediaQuery"+e]=window.matchMedia(f),eventObject.addListener(function(){mob.resetBooks()})}},setShowBooks:function(){this.__book.length&&(clearTimeout(this.scrollTimer),this.scrollTimer=setTimeout(function(){mob.showBooks()},150))}},window.addEventListener("load",function(){mob.touch()})})();