Created
August 31, 2012 18:34
-
-
Save carlosrberto/3557041 to your computer and use it in GitHub Desktop.
Background Resize
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
$(function(){ | |
var image = $('.background'); | |
function resize(){ | |
resizeBackground(image, 640, 480); | |
} | |
$(window).resize(resize); | |
}); |
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
.background{ position:absolute; left:0; top:0; } | |
.full-width{ min-width:100%; } | |
.full-height{ min-height:100%; } |
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
/** | |
* 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