diff --git a/responsify.js b/responsify.js index 2b136ed..283ef53 100644 --- a/responsify.js +++ b/responsify.js @@ -5,16 +5,21 @@ twidth, theight, fx1, fy1, fx2, fy2, width, height, top, left, - $this = $(this); + $this = $(this), + isImg = $this.is('img'); - owidth = $this.width(); - oheight = $this.height(); - twidth = $this.parent().width(); - theight = $this.parent().height(); fx1 = Number($this.attr('data-focus-left')); fy1 = Number($this.attr('data-focus-top')); fx2 = Number($this.attr('data-focus-right')); fy2 = Number($this.attr('data-focus-bottom')); + + if(isNaN(fx1) || isNaN(fy1) || isNaN(fx2) || isNaN(fy2)) return; + + owidth = ( isImg ? $this.width() : $this.attr('data-focus-width') ); + oheight = ( isImg ? $this.height() : $this.attr('data-focus-height') ); + twidth = ( isImg ? $this.parent().width() : $this.width() ); + theight = ( isImg ? $this.parent().height() : $this.height() ); + if( owidth/oheight > twidth/theight ) { var fwidth = (fx2-fx1) * owidth; if ( fwidth/oheight > twidth/theight ) { @@ -51,16 +56,23 @@ left = 0; } } - $this.parent().css({ - "overflow": "hidden" - }) - $this.css({ - "position": "relative", - "height": height, - "width": width, - "left": left, - "top": top - }) + if( isImg ){ + $this.parent().css({ + "overflow": "hidden" + }) + $this.css({ + "position": "relative", + "height": height, + "width": width, + "left": left, + "top": top + }) + }else{ + $this.css({ + "background-size": width + 'px ' + height + 'px', + "background-position": left + 'px ' + top + 'px' + }); + } }); }; }( jQuery )); diff --git a/responsify.min.js b/responsify.min.js index c163838..f1f1f53 100644 --- a/responsify.min.js +++ b/responsify.min.js @@ -1 +1 @@ -!function(t){t.fn.responsify=function(){return this.each(function(){var e,r,a,i,o,h,n,s,f,u,c,d,p=t(this);if(e=p.width(),r=p.height(),a=p.parent().width(),i=p.parent().height(),o=Number(p.attr("data-focus-left")),h=Number(p.attr("data-focus-top")),n=Number(p.attr("data-focus-right")),s=Number(p.attr("data-focus-bottom")),e/r>a/i){var b=(n-o)*e;b/r>a/i?(u=r*a/b,f=e*a/b,d=-o*f,c=(i-u)/2):(u=i,f=i*e/r,d=a/2-(o+n)*f/2,d=d>0?0:d,d=a-d-f>0?a-f:d,c=0)}else{var l=(s-h)*r;l/e>i/a?(f=e*i/l,u=r*i/l,c=-h*u,d=(a-f)/2):(f=a,u=a*r/e,c=i/2-(h+s)*u/2,c=c>0?0:c,c=i-c-u>0?i-u:c,d=0)}p.parent().css({overflow:"hidden"}),p.css({position:"relative",height:u,width:f,left:d,top:c})})}}(jQuery); +!function(t){t.fn.responsify=function(){return this.each(function(){var a,i,e,r,s,o,h,n,u,f,c,d,p=t(this),N=p.is("img");if(s=Number(p.attr("data-focus-left")),o=Number(p.attr("data-focus-top")),h=Number(p.attr("data-focus-right")),n=Number(p.attr("data-focus-bottom")),!(isNaN(s)||isNaN(o)||isNaN(h)||isNaN(n))){if(a=N?p.width():p.attr("data-focus-width"),i=N?p.height():p.attr("data-focus-height"),e=N?p.parent().width():p.width(),r=N?p.parent().height():p.height(),a/i>e/r){var g=(h-s)*a;g/i>e/r?(d=-s*(u=a*e/g),c=(r-(f=i*e/g))/2):(f=r,d=e-(d=(d=e/2-(s+h)*(u=r*a/i)/2)>0?0:d)-u>0?e-u:d,c=0)}else{var b=(n-o)*i;b/a>r/e?(c=-o*(f=i*r/b),d=(e-(u=a*r/b))/2):(u=e,c=r-(c=(c=r/2-(o+n)*(f=e*i/a)/2)>0?0:c)-f>0?r-f:c,d=0)}N?(p.parent().css({overflow:"hidden"}),p.css({position:"relative",height:f,width:u,left:d,top:c})):p.css({"background-size":u+"px "+f+"px","background-position":d+"px "+c+"px"})}})}}(jQuery);