开发者

YouTube custom embed sizer

开发者 https://www.devze.com 2023-03-14 04:24 出处:网络
On a site I am building, I need to allow the video (iframe embed) to dynamically stretch to fit the screen. I will be grabbing the width of the parent div with jQuery, but I need to work out the math

On a site I am building, I need to allow the video (iframe embed) to dynamically stretch to fit the screen. I will be grabbing the width of the parent div with jQuery, but I need to work out the math to automatically work out the height size.

YouTube has managed to do this in their custom sizer (click share, then embed and fill in the width to see the height automatically fill in).

Any ideas would be really useful, thanks!


Edit

Thanks to Eray for the help with this! If anyone is interested, here is the final working code (I think it works, anyway!):

$documentWidth = $('#wrap').width();
$theWidth = parseInt($documentWidth, 开发者_C百科10);
$theHeight = (Math.floor($theWidth / 1.755) || 0) + 30;
$('#video iframe').height($theHeight);
$('#video iframe').width($documentWidth);

I am still not 100% sure how it works!


Get original width/height ratio . And then use this formula :

New Width = New Height * (Original Width/Original Height)

So you will keep same ratio on new width height combination.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号