forked from gestixi/image-scale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image-scale.min.js
executable file
·10 lines (10 loc) · 4.5 KB
/
image-scale.min.js
1
2
3
4
5
6
7
8
9
10
/*! image-scale v2.1 | ©2012-2016 GestiXi | Licensed under the MIT license */
!function(l){l.fn.imageScale=function(b){return this.each(function(){var f=this,c=l(this),a=c.data("imageScale"),g="IMG"===this.tagName?c:c.find("img");if(a)if("string"==typeof b)a[b]();else if("object"==typeof b)a[b.method||"scale"](!1,b);else a.scale();else{var e=g[0].complete,k=l.extend({},l.fn.imageScale.defaults,"object"==typeof b&&b),h=function(){c.data("imageScale",a=new n(f,k));a.scale(!0,k)};e?h.apply(c[0]):g.on("load",h).attr("src",g.attr("src"))}})};l.fn.imageScale.defaults={scale:"best-fill",
align:"center",parent:null,hideParentOverflow:!0,fadeInDuration:0,rescaleOnResize:!1,didScale:function(b,f){},logLevel:0};var n=function(b,f){var c=this;c.options=f;c.element=b;var a=c.$element=l(b),g=c.$img="IMG"===b.tagName?a:a.find("img"),e=c.img=g[0];c.src=g.attr("src");c.imgWidth=e.naturalWidth||e.width;c.imgHeight=e.naturalHeight||e.height;a=c.$parent=f.parent?f.parent:l(a.parent()[0]);c.parent=a[0];"static"===a.css("position")&&a.css("position","relative");f.rescaleOnResize&&l(window).resize(function(a){c.scheduleScale()})};
l.fn.imageScale.Constructor=n;n.prototype={NONE:"none",FILL:"fill",BEST_FILL:"best-fill",BEST_FIT:"best-fit",BEST_FIT_DOWN_ONLY:"best-fit-down",ALIGN_LEFT:"left",ALIGN_RIGHT:"right",ALIGN_CENTER:"center",ALIGN_TOP:"top",ALIGN_BOTTOM:"bottom",ALIGN_TOP_LEFT:"top-left",ALIGN_TOP_RIGHT:"top-right",ALIGN_BOTTOM_LEFT:"bottom-left",ALIGN_BOTTOM_RIGHT:"bottom-right",constructor:n,element:null,options:null,scale:function(b,f){if(!this._isDestroyed&&!1!==this._canScale){var c=this,a=this.options,g=this.$parent,
e=this.element,k=this.$element,h=this.$img;if(b)a.hideParentOverflow&&g.css({overflow:"hidden"});else if(this.src!==h.attr("src")){this.destroy();k.data("imageScale",null);k.imageScale(a);return}this._didScheduleScale=!1;if(!a.rescaleOnResize||f||this._needUpdate(this.parent)){f=f?f:{};if(h=f.transition)this._canScale=!1,k.css("transition","all "+h+"ms"),setTimeout(function(){c._canScale=null;k.css("transition","null")},h);var h=f.destWidth?f.destWidth:g.outerWidth(),d=f.destHeight?f.destHeight:g.outerHeight(),
l=f.destWidth?f.destWidth:g.innerWidth(),m=f.destHeight?f.destHeight:g.innerHeight(),g=h-l,l=d-m,m=k.attr("data-scale"),p=k.attr("data-align"),m=m?m:a.scale,n=p?p:a.align,p=a.fadeInDuration;if(m){this._cacheDestWidth===h&&this._cacheDestHeight===d&&2<a.logLevel&&console.log("imageScale - DEBUG NOTICE: The parent size hasn't changed: dest width: '"+h+"' - dest height: '"+d+"'.",e);var q=this.imgWidth,r=this.imgHeight;h&&d&&q&&r?(this._cacheDestWidth=h,this._cacheDestHeight=d,e=this._innerFrameForSize(m,
n,q,r,h,d),g&&(e.x-=g/2),l&&(e.y-=l/2),k.css({position:"absolute",top:e.y+"px",left:e.x+"px",width:e.width+"px",height:e.height+"px","max-width":"none"}),b&&p&&(k.css({display:"none"}),k.fadeIn(p)),a.didScale.call(this,b,f)):0<a.logLevel&&console.error("imageScale - DEBUG ERROR: The dimensions are incorrect: source width: '"+q+"' - source height: '"+r+"' - dest width: '"+h+"' - dest height: '"+d+"'.",e)}else 2<a.logLevel&&console.log("imageScale - DEBUG NOTICE: The scale property is null.",e)}}},
destroy:function(){this._isDestroyed=!0;this.$element.removeData("imageScale")},_innerFrameForSize:function(b,f,c,a,g,e){var k,h,d;d={x:0,y:0,width:g,height:e};if(b===this.FILL)return d;k=g/c;h=e/a;switch(b){case this.BEST_FIT_DOWN_ONLY:b!==this.BEST_FIT_DOWN_ONLY&&1<this.options.logLevel&&console.warn("imageScale - DEBUG WARNING: The scale '"+b+"' was not understood.");b=c>g||a>e?k<h?k:h:1;break;case this.BEST_FIT:b=k<h?k:h;break;case this.NONE:b=1;break;default:b=k>h?k:h}c*=b;a*=b;d.width=Math.round(c);
d.height=Math.round(a);switch(f){case this.ALIGN_LEFT:d.x=0;d.y=e/2-a/2;break;case this.ALIGN_RIGHT:d.x=g-c;d.y=e/2-a/2;break;case this.ALIGN_TOP:d.x=g/2-c/2;d.y=0;break;case this.ALIGN_BOTTOM:d.x=g/2-c/2;d.y=e-a;break;case this.ALIGN_TOP_LEFT:d.x=0;d.y=0;break;case this.ALIGN_TOP_RIGHT:d.x=g-c;d.y=0;break;case this.ALIGN_BOTTOM_LEFT:d.x=0;d.y=e-a;break;case this.ALIGN_BOTTOM_RIGHT:d.x=g-c;d.y=e-a;break;default:f!==this.ALIGN_CENTER&&1<this.options.logLevel&&console.warn("imageScale - DEBUG WARNING: The align '"+
f+"' was not understood."),d.x=g/2-c/2,d.y=e/2-a/2}return d},_needUpdate:function(b){b=b.clientHeight+" "+b.clientWidth;return this._lastParentSize!==b?(this._lastParentSize=b,!0):!1},scheduleScale:function(){if(!this._didScheduleScale)if(window.requestAnimationFrame){var b=this;this._didScheduleScale=!0;requestAnimationFrame(function(){setTimeout(function(){b.scale()},0)})}else this.scale()}}}(window.jQuery);