Skip to content

Instantly share code, notes, and snippets.

@lzl124631x

lzl124631x/js

Last active Aug 11, 2016
Embed
What would you like to do?
keep element's ratio
// http://codepen.io/lzl124631x/pen/xOmJGE
$.fn.keepRatio = function(ratio) {
var self = this,
$p = this.parent(),
keep = function() {
var w = $p.width(),
h = $p.height();
if (w / h > ratio) {
self.height(h);
self.width(h * ratio);
} else {
self.width(w);
self.height(w / ratio);
}
};
$p.on('resize', keep);
keep();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.