Skip to content

Instantly share code, notes, and snippets.

@lzl124631x
Last active August 11, 2016 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lzl124631x/767a27380ebd9aa84d9c47cf1a73f6be to your computer and use it in GitHub Desktop.
Save lzl124631x/767a27380ebd9aa84d9c47cf1a73f6be to your computer and use it in GitHub Desktop.
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