Last active
August 29, 2015 13:55
-
-
Save gbyat/8745709 to your computer and use it in GitHub Desktop.
Keep min-width, max-width and aspect ratio width jQuery
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
body { | |
margin:0; | |
padding:0; | |
} | |
* { | |
-webkit-box-sizing: border-box; | |
-moz-box-sizing: border-box; | |
-ms-box-sizing: border-box; | |
-o-box-sizing: border-box; | |
box-sizing: border-box; | |
} | |
#aspectratio { | |
position: relative; | |
margin:auto; | |
background: rgb(96,108,136); /* Old browsers */ | |
background: -moz-linear-gradient(top, rgba(96,108,136,1) 0%, rgba(63,76,107,1) 100%); /* FF3.6+ */ | |
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(96,108,136,1)), color-stop(100%,rgba(63,76,107,1))); /* Chrome,Safari4+ */ | |
background: -webkit-linear-gradient(top, rgba(96,108,136,1) 0%,rgba(63,76,107,1) 100%); /* Chrome10+,Safari5.1+ */ | |
background: -o-linear-gradient(top, rgba(96,108,136,1) 0%,rgba(63,76,107,1) 100%); /* Opera 11.10+ */ | |
background: -ms-linear-gradient(top, rgba(96,108,136,1) 0%,rgba(63,76,107,1) 100%); /* IE10+ */ | |
background: linear-gradient(to bottom, rgba(96,108,136,1) 0%,rgba(63,76,107,1) 100%); /* W3C */ | |
} | |
#aspectratio > div { | |
position: absolute; | |
top: 3%; | |
left: 2.5%; | |
bottom:3%; | |
right:2.5%; | |
padding:2% 3%; | |
background:white; | |
background:rgba(255, 255, 255, 0.4); | |
} |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>jQuery aspect ratio for a div</title> | |
<meta name="description" content="jQuery function to keep aspect ratio for a div on height and width resizing"> | |
<link rel="stylesheet" href="aspectratio.css"> | |
</head> | |
<body> | |
<div id="aspectratio"> | |
<div> | |
playground | |
</div> | |
</div> | |
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> | |
<script src="aspectratio.js"></script> | |
</body> | |
</html> |
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
jQuery(document).ready(function() { jQuery(window).resize(); }); | |
jQuery(window).resize(function() { | |
var divheight = $(window).height(); | |
var divwidth = (divheight / 9) * 16; | |
var maxwidth = $('body').width(); | |
if ( divwidth > maxwidth ) { | |
var wrapperwidth = maxwidth; | |
} else { | |
var wrapperwidth = divwidth; | |
} | |
var maxheight = (wrapperwidth / 16) * 9; | |
if ( divheight > maxheight ) { | |
var wrapperheight = maxheight; | |
} else { | |
var wrapperheight = divheight; | |
} | |
$("#aspectratio").css('width', wrapperwidth); | |
$("#aspectratio").css('height', wrapperheight); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment