Skip to content

Instantly share code, notes, and snippets.

@muhittin
Created August 25, 2012 10:01
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 muhittin/3463173 to your computer and use it in GitHub Desktop.
Save muhittin/3463173 to your computer and use it in GitHub Desktop.
resize element to screen resolution (with jQuery)
function resize(oran){
var newH = parseFloat( $(window).height() );
var newW = parseFloat( $(window).width() );
var rat = newW / newH;
if(rat > oran)
{
var targetHeight = newH;
var targetWidth = newH * oran;
}
else
{
var targetWidth = newW;
var targetHeight = newW / oran;
}
var targetTop = (newH - targetHeight) / 2;
var targetLeft = (newW - targetWidth) / 2;
return {width: targetWidth, height: targetHeigt, top: targetTop, left: targetLeft};
}
// Usage:
var props = resize(4/3);
$("#myDiv").css(props);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment