forked from shprink/BttrLazyLoading
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.bttrlazyloading.min.js
10 lines (8 loc) · 7.2 KB
/
jquery.bttrlazyloading.min.js
1
2
3
4
5
6
7
8
9
10
/*
BttrLazyLoading, Responsive Lazy Loading plugin for JQuery
by Julien Renaux http://bttrlazyloading.julienrenaux.fr
Version 1.0.4
Full source at https://github.com/shprink/BttrLazyLoading
MIT License, https://github.com/shprink/BttrLazyLoading/blob/master/LICENSE
*/
(function(){"use strict";var t,i,n;t=jQuery,i=function(){function i(i,n){var o,r=this;null==n&&(n={}),this.$img=t(i),this.loaded=!1,this.loading=!1,o=t.extend(!0,{},t.bttrlazyloading.constructor.options),this.options=t.extend(!0,o,n),this.ranges=t.bttrlazyloading.constructor.ranges,this.$container=t(this.options.container),"number"==typeof window.devicePixelRatio&&(this.constructor.dpr=window.devicePixelRatio),this.whiteList=["lg","md","sm","xs"],this.blackList=[],h.call(this),this.$wrapper=t('<span class="bttrlazyloading-wrapper">'),this.options.wrapperClasses&&"string"==typeof this.options.wrapperClasses&&this.$wrapper.addClass(this.options.wrapperClasses),this.$img.before(this.$wrapper),this.$clone=t('<canvas class="bttrlazyloading-clone">'),g.call(this),this.$wrapper.append(this.$clone),this.$img.hide(),this.$wrapper.append(this.$img),this.options.backgroundcolor&&this.$wrapper.css("background-color",this.options.backgroundcolor),A.call(this,"on"),setTimeout(function(){return d.call(r)},100)}var n,o,r,e,s,a,l,h,A,d,g;return i.dpr=1,g=function(){var t;return t=o.call(this),this.$clone.attr("width",t.width),this.$clone.attr("height",t.height)},h=function(){var i=this;return t.each(this.$img.data(),function(n,o){if(o)if(0!==n.indexOf("bttrlazyloading"),n=n.replace("bttrlazyloading","").replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase().split("-"),n.length>1){if("undefined"!=typeof i.options[n[0]][n[1]])return i.options[n[0]][n[1]]=o}else{if("object"==typeof o)return t.extend(i.options[n[0]],o);if("undefined"!=typeof i.options[n[0]])return i.options[n[0]]=o}})},A=function(i){var r,e,s,a,l=this;return s=function(){return l.$clone.hide(),l.$img.show(),l.$img.addClass("bttrlazyloading-loaded"),l.options.animation&&l.$img.addClass("animated "+l.options.animation),l.loaded=l.$img.attr("src"),l.$img.trigger("bttrlazyloading.afterLoad")},this.$img[i]("load",s),r=function(){var t;return l.loading?void 0:(l.loading=!0,t=o.call(l),l.loaded?(l.$img.removeClass("bttrlazyloading-loaded"),l.options.animation&&l.$img.removeClass("animated "+l.options.animation),l.$img.removeAttr("src"),l.$img.hide(),l.$clone.attr("width",t.width),l.$clone.attr("height",t.height),l.$clone.show()):l.$wrapper.css("background-image","url('"+l.options.placeholder+"')"),setTimeout(function(){return l.$img.trigger("bttrlazyloading.beforeLoad"),l.$img.data("bttrlazyloading.range",t.range),l.$img.attr("src",n.call(l,t.src,t.range)),l.loading=!1},l.options.delay))},this.$img[i]("bttrlazyloading.load",r),e=function(){var t,i;if(i=l.$img.attr("src"),t=l.$img.data("bttrlazyloading.range"),l.constructor.dpr>=2&&l.options.retina&&i.match(/@2x/gi))l.blackList.push(t+"@2x");else if(l.blackList.push(t),l.whiteList.splice(l.whiteList.indexOf(t),1),0===l.whiteList.length)return l.$img.trigger("bttrlazyloading.error"),!1;return l.$img.trigger("bttrlazyloading.load")},this.$img[i]("error",e),a=function(){return d.call(l)},this.$container[i](this.options.event,a),this.options.container!==window&&t(window)[i](this.options.event,a),t(window)[i]("resize",a)},s=function(){var t;return t=window.innerWidth,t<=this.ranges.xs?"xs":this.ranges.sm<=t&&t<this.ranges.md?"sm":this.ranges.md<=t&&t<this.ranges.lg?"md":this.ranges.lg<=t?"lg":void 0},o=function(){return this.range=s.call(this),e.call(this)},n=function(t,i){return this.constructor.dpr>=2&&this.options.retina&&-1===this.blackList.indexOf(i+"@2x")?t.replace(/\.\w+$/,function(t){return"@2x"+t}):t},r=function(t){return"undefined"!=typeof this.options[t].src&&null!==this.options[t].src?this.options[t]:null},e=function(){var t,i,n,o,e,s;if(t=this.whiteList.indexOf(this.range),t>-1&&(n=r.call(this,this.range)))return n.range=this.range,n;for(s=this.whiteList,t=o=0,e=s.length;e>o;t=++o)if(i=s[t],n=r.call(this,i))return n.range=i,n;return""},a=function(){var i,r,e;return!this.loaded&&this.options.triggermanually?!1:this.loaded&&this.options.updatemanually?!1:(i=o.call(this),i.src&&this.loaded!==n.call(this,i.src,i.range)?(e=0,this.loaded||(e=this.options.threshold),r=l.call(this,t(window),{top:t(window).scrollTop()+e,left:t(window).scrollLeft()}),this.options.container!==window?r&&l.call(this,this.$container,{top:this.$container.offset().top+e,left:this.$container.offset().left}):r):!1)},l=function(t,i){var n;return null==i&&(i={}),i.right=i.left+t.width(),i.bottom=i.top+t.height(),n=this.$wrapper.offset(),n.right=n.left+this.$wrapper.outerWidth(),n.bottom=n.top+this.$wrapper.outerHeight(),!(i.right<n.left||i.left>n.right||i.bottom<n.top||i.top>n.bottom)},d=function(){return this.range!==s.call(this)&&g.call(this),a.call(this)?this.$img.trigger("bttrlazyloading.load"):void 0},i.prototype.get$Img=function(){return this.$img},i.prototype.get$Clone=function(){return this.$clone},i.prototype.get$Wrapper=function(){return this.$wrapper},i.prototype.destroy=function(){return this.$wrapper.before(this.$img),this.$wrapper.remove(),A.call(this,"off"),this.$img.off("bttrlazyloading"),this.$img.removeClass("bttrlazyloading-loaded"),this.options.animation&&this.$img.removeClass("animated "+this.options.animation),this.$img.removeData("bttrlazyloading"),this.$img},i}(),t.fn.extend({bttrlazyloading:function(n){return this.each(function(){var o,r;return o=t(this),r=o.data("bttrlazyloading"),"undefined"==typeof r&&(r=new i(this,n),o.data("bttrlazyloading",r)),"string"==typeof n&&"undefined"!=typeof r[n]?r[n].call(r):void 0})}}),t.fn.bttrlazyloading.Constructor=i,n=function(){function i(){}return i.prototype.version="1.0.3",i.ranges={xs:767,sm:768,md:992,lg:1200},i.options={xs:{src:null,width:100,height:100},sm:{src:null,width:100,height:100},md:{src:null,width:100,height:100},lg:{src:null,width:100,height:100},retina:!1,animation:"bounceIn",delay:0,event:"scroll",container:window,threshold:0,triggermanually:!1,updatemanually:!1,wrapperClasses:null,backgroundcolor:"#EEE",placeholder:"data:image/gif;base64,R0lGODlhEAALAPQAAP/391tbW+bf3+Da2vHq6l5dXVtbW3h2dq6qqpiVldLMzHBvb4qHh7Ovr5uYmNTOznNxcV1cXI2Kiu7n5+Xf3/fw8H58fOjh4fbv78/JycG8vNzW1vPs7AAAAAAAAAAAACH/C05FVFNDQVBFMi4wAwEAAAAh/hpDcmVhdGVkIHdpdGggYWpheGxvYWQuaW5mbwAh+QQJCwAAACwAAAAAEAALAAAFLSAgjmRpnqSgCuLKAq5AEIM4zDVw03ve27ifDgfkEYe04kDIDC5zrtYKRa2WQgAh+QQJCwAAACwAAAAAEAALAAAFJGBhGAVgnqhpHIeRvsDawqns0qeN5+y967tYLyicBYE7EYkYAgAh+QQJCwAAACwAAAAAEAALAAAFNiAgjothLOOIJAkiGgxjpGKiKMkbz7SN6zIawJcDwIK9W/HISxGBzdHTuBNOmcJVCyoUlk7CEAAh+QQJCwAAACwAAAAAEAALAAAFNSAgjqQIRRFUAo3jNGIkSdHqPI8Tz3V55zuaDacDyIQ+YrBH+hWPzJFzOQQaeavWi7oqnVIhACH5BAkLAAAALAAAAAAQAAsAAAUyICCOZGme1rJY5kRRk7hI0mJSVUXJtF3iOl7tltsBZsNfUegjAY3I5sgFY55KqdX1GgIAIfkECQsAAAAsAAAAABAACwAABTcgII5kaZ4kcV2EqLJipmnZhWGXaOOitm2aXQ4g7P2Ct2ER4AMul00kj5g0Al8tADY2y6C+4FIIACH5BAkLAAAALAAAAAAQAAsAAAUvICCOZGme5ERRk6iy7qpyHCVStA3gNa/7txxwlwv2isSacYUc+l4tADQGQ1mvpBAAIfkECQsAAAAsAAAAABAACwAABS8gII5kaZ7kRFGTqLLuqnIcJVK0DeA1r/u3HHCXC/aKxJpxhRz6Xi0ANAZDWa+kEAA7AAAAAAAAAAAA"},i.prototype.setOptions=function(i){return null==i&&(i={}),t.extend(!0,this.constructor.options,i),this},i.prototype.setRanges=function(i){return null==i&&(i={}),t.extend(!0,this.constructor.ranges,i),this},i}(),t.bttrlazyloading=new n}).call(this);