Skip to content

Instantly share code, notes, and snippets.

@prisme
Created April 30, 2013 14:03
Show Gist options
  • Save prisme/5488914 to your computer and use it in GitHub Desktop.
Save prisme/5488914 to your computer and use it in GitHub Desktop.
"full-frame" resize handling jQuery plugin : resizing/centering/cropping an asset in its parent, according to their respective ratio. parent needs an overflow:hidden
$.fn.centerIn = function (parent) {
var parentHeight = parent.height(),
parentWidth = parent.width(),
assetHeight = this.height(),
assetWidth = this.width(),
parentRatio = parentWidth / parentHeight,
assetRatio = assetWidth / assetHeight;
//----> ratio
if ( parentRatio < imgRatio ) {
this.css({ "height" : "100%" , "width" : "auto" });
} else {
this.css({ "width" : "100%" , "height" : "auto" });
}
//----> center
this.css({
"left" : ( parentWidth - assetWidth ) / 2
, "top" : ( parentHeight - assetHeight ) / 2
});
return this;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment