简体   繁体   English

调整视频iframe的高度

[英]Adjust height on iframe of video

I am using TinyMCE as my WYSIWYG text editor, so iframes are created with no tags. 我将TinyMCE用作所见即所得的文本编辑器,因此创建的iframe没有任何标签。 The solutions I have online all use jQuery to adjust iframes with particular tags, but how can I adjust all iframes on the page? 我在线上所有的解决方案都使用jQuery来调整带有特定标签的iframe,但是如何调整页面上的所有iframe? I have tried using CSS for the width: 100% and it works correctly, but when I add height: auto it does not adjust ajust correspondingly. 我尝试使用CSS来设置width: 100% ,并且可以正常工作,但是当我添加height: auto它不会相应地进行调整。

How can I make it so that the untagged iframe adjusts its height automatically to the width of the page? 如何使未加标签的iframe自动将其高度调整为页面的宽度?

There's no way to specify this with CSS, the iframe needs to resolve to an absolute height (and the content of the iframe will adjust to the size of the iframe, not the other way around). 无法使用CSS进行指定,iframe需要解析为绝对高度(iframe 的内容将调整为iframe的大小,而不是相反)。

However, if what you're embedding is relatively predictable, in that the aspect ratio of the videos you're embedding is constant, here's an alternative by using a wrapper div over the iframe : 但是,如果您要嵌入的内容是相对可预测的,则因为您要嵌入的视频的长宽比是恒定的,那么可以通过在iframe使用wrapper div来实现此选择:

HTML HTML

<div class='video-wrapper'>
  <iframe ...></iframe>
</div>

CSS CSS

.video-wrapper {
  position: relative;
  height: 0;
  padding-bottom: 56.25%;
}

.video-wrapper iframe {
   position: absolute;
   width: 100%;
   height: 100%;
   top: 0;
   left: 0;
}

You plug in your aspect ratio in the padding-bottom property of the wrapper div, and it is computed by the formula: video_height / video_width * 100 . 您将长宽比插入包装div的padding-bottom属性中,并通过以下公式计算: video_height / video_width * 100 For example, a video with the 16:9 aspect ratio that gives you 9 / 16 * 100 = 56.25% . 例如,视频纵横比为16:9的视频可让您获得9 / 16 * 100 = 56.25%

Now, the tricky part is getting the wrapper <div> inside TinyMCE, and that might not be possible unless editing the underlying HTML. 现在,棘手的部分是在TinyMCE中获取包装器<div> ,除非编辑基础HTML,否则可能无法实现。 The alternative would be to use jQuery to wrap all iframes inside divs: 替代方法是使用jQuery将所有iframe包裹在div中:

$('iframe').wrap('<div class="video-wrapper"></div>');

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM