Created
April 30, 2013 14:03
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.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