-
-
Save lzl124631x/767a27380ebd9aa84d9c47cf1a73f6be to your computer and use it in GitHub Desktop.
keep element's ratio
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
// 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