-
Notifications
You must be signed in to change notification settings - Fork 0
/
bis.min.js
1 lines (1 loc) · 2.18 KB
/
bis.min.js
1
function BIS(t){this.el=t,this.has=this.source=this.has(t)}function bis(t){return new BIS(t)}BIS.prototype.has=function(){return BIS.has(this.el)},BIS.has=function(t){var e=t.css("background-image");if(!e||"none"==e.toLowerCase())return!1;if(0===e.indexOf("url(")&&e.indexOf(")")===e.length-1){var i=e.slice(4,e.length-1);return'"'==i[0]||"'"==i[0]?i.slice(1,i.length-1):i}return e},BIS.prototype.getImageRatio=function(t,e){return this.source?void BIS.getImageRatio(this.source,t,e):null},BIS.prototype.getElementRatio=function(t,e){var i=BIS.getElementRatio(this.el,t,e);return t?void 0:i},BIS.getElementRatio=function(t,e,i){var n=t.outerWidth()/t.outerHeight();return e?void e(n,i):n},BIS.getImageRatio=function(t,e){BIS.getImageSize(t,function(t,i){e(t.width/t.height,i)})},BIS.prototype.getImageSize=function(t,e){this.source||t(null,e),BIS.getImageSize(this.source,t,e)},BIS.getImageSize=function(t,e,i){var n=new Image;n.onload=function(){e({width:n.width,height:n.height},i)},n.src=t},BIS.prototype.get=function(t,e){BIS.get(this.el,t,e)},BIS.get=function(t,e,i){if(!t)return null;t=$(t instanceof jQuery?t.get(0):$(t).get(0));var n=BIS.has(t);if(!n)return null;var o,r,h,a,u,g,s=t.css("background-size").toLowerCase(),I={};"cover"==s?BIS.getImageRatio(n,function(n){g=BIS.getElementRatio(t),o=t.outerWidth(),r=t.outerHeight(),g>=n?(I.width=o,I.height=Math.floor(o/n)):(I.width=r*n,I.height=r),e(I,i)}):"contain"==s?BIS.getImageRatio(n,function(n){g=BIS.getElementRatio(t),o=t.outerWidth(),r=t.outerHeight(),g>=n?(I.width=r*n,I.height=r):(I.width=o,I.height=Math.floor(o/n)),e(I,i)}):"auto"==s||"initial"==s||"auto auto"==s?BIS.getImageSize(n,function(t){e(t,i)}):-1!=s.indexOf("%")||-1!=s.indexOf("px")?BIS.getImageSize(n,function(n){var g=function(t,e,i){return e/t*i};u=s.split(" "),1==u.length&&(u[1]="auto"),"auto"==u[0].toLowerCase()?I.width=n.width:-1!=s.indexOf("%")?(h=parseInt(u[0].replace("%","")),o=t.outerWidth(),I.width=g(100,o,h)):I.width=parseInt(u[0].replace("px","")),"auto"==u[1].toLowerCase()?I.height=n.height:-1!=s.indexOf("%")?(a=parseInt(u[1].replace("%","")),r=t.outerHeight(),I.height=g(100,r,a)):I.height=parseInt(u[1].replace("px","")),e(I,i)}):e({width:t.outerWidth(),height:t.outerHeight()},i)};