Skip to content

Instantly share code, notes, and snippets.

@carlosrberto
Created August 31, 2012 18:34
Show Gist options
  • Save carlosrberto/3557041 to your computer and use it in GitHub Desktop.
Save carlosrberto/3557041 to your computer and use it in GitHub Desktop.
Background Resize
$(function(){
var image = $('.background');
function resize(){
resizeBackground(image, 640, 480);
}
$(window).resize(resize);
});
.background{ position:absolute; left:0; top:0; }
.full-width{ min-width:100%; }
.full-height{ min-height:100%; }
/**
* Redimensiona a imagem de acordo com o tamanho da janela
* @param {jQueryObject} image
* @param {Integer} imgWidth
* @param {Integer} imgHeight
*/
function resizeBackground(image, imgWidth, imgHeight){
var win = $(window),
winWidth = win.width(),
winHeight = win.height(),
aspectRatio = winWidth/winHeight;
imgAspectRatio = imgWidth/imgHeight;
if ( aspectRatio < imgAspectRatio ) {
image.removeClass('full-width').addClass('full-height');
} else if ( aspectRatio > imgAspectRatio ) {
image.removeClass('full-height').addClass('full-width');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment