If I were to embed a YouTube video for example
The best way is to simply use CSS because u can keep
it responsive as the page loads and not have a visible
'jump' in the iframe width and height.
Also, you don't have to set a fixed width (although u can)
like this:
iframe {width: 100%; height: calc(~'100% * 9/16');}
$('.player').parent().attr('width', 560*9/16)
Aspect ratio is just width:height. So if you wanted to calculate the height based on a known width it is pretty straightforward.
//width=560, wanted height is 315
$('.player').parent().attr('height', 560*9/16);
Pretty old question but if somebody still seeking for the answer, this one could be a better approach.
function getRelativeWidth(ratio, crntHght) { ratio = ratio.split(":"); var wdthRto = ratio[0]; var hghtRto = ratio[1]; return ((wdthRto*crntHght) / hghtRto); } function getRelativeHeight(ratio, crntWdth) { ratio = ratio.split(":"); var wdthRto = ratio[0]; var hghtRto = ratio[1]; return ((crntWdth*hghtRto) / wdthRto); } var wdth = 1600; var hght = 900; var getHeight = getRelativeHeight("16:9", wdth); var getWeight = getRelativeWidth("16:9", hght); console.log(getHeight); console.log(getWeight);
I would recommend using css calc instead of jQuery for this:
width: 560px;
height: calc(560px * 9/16);