Skip to content

Instantly share code, notes, and snippets.

@stresslimit
Created February 19, 2011 23:45
Show Gist options
  • Save stresslimit/835514 to your computer and use it in GitHub Desktop.
Save stresslimit/835514 to your computer and use it in GitHub Desktop.
jquery bg image resizer
<script>
$(document).ready(resizebg);
$(window).resize(resizebg);
function resizebg() {
var w,h,x,y;
var ratio = 4/3;
x = $(window).width();
y = $(window).height();
var constrain = x/y > ratio ? 'w' : 'h';
if(constrain=='w') {
w = x;
h = Math.round(x*.75);
} else if(constrain=='h') {
w = Math.round(y*1.333);
h = y;
}
$('#bg').css({ 'width': w+'px', 'height': h+'px'});
}
</script>
<style>
#bg { position:absolute; bottom:0; right:0; z-index:0; }
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment