简体   繁体   English

调整浏览器大小时保持视频的纵横比

[英]Maintain aspect ratio of a video when resizing the browser

I'm working on a Video editing tool, and I need to maintain the 16:9 aspect ratio of the video when resizing the screen horizontally and vertically.我正在开发一个视频编辑工具,在水平和垂直调整屏幕大小时,我需要保持视频的16:9纵横比。 So far I got it to work as expected when resizing horizontally, and when resizing down vertically, but can't get it to work when sizing up vertically .到目前为止,我水平调整时,和垂直缩放下来的时候,但如果不能得到它的工作,它得到的工作如预期垂直估量 The Javascript code I used to calculate the height of the video and resize it is below (notice how the else clause is empty because that's where the code should go):我用来计算视频高度和调整大小的 Javascript 代码如下(注意else子句是空的,因为那是代码应该去的地方):

const calculateHeight = () => {
    // Get the other elements on the page
    const header = document.querySelector('.main-navigation');
    const meshTopBar = document.querySelector('.mesh__top-bar');
    const footer = document.querySelector('.mesh__bottom-bar');
    // Get the section to apply the window height to it
    const mainSection = document.querySelector('.insert-level-container');
    // Get the video elements
    const editor = document.querySelector('.mesh__insert-editor-container');
    const video = document.querySelector('.mesh__insert-editor-video-container');

    // Apply the height to the main section by calculating the window height minus the other elements' height
    if(mainSection !== null) {
      mainSection.style.height = (window.innerHeight - header.offsetHeight - meshTopBar.offsetHeight - footer.offsetHeight) + 'px';
    }

    // This should be the ideal height for the video
    video.style.minHeight = ((video.offsetWidth * 9) / 16) + 'px';

    // If the video height is bigger than the section height (calculated above), then resize it
    if(video.offsetHeight + 115 > mainSection.offsetHeight) {
      video.style.minHeight = video.offsetHeight - 1 + 'px';
      editor.style.maxWidth = video.offsetHeight * 16 / 9 + 'px';
    } else {
      // This is where the logic for the vertical resizing should go
    }
  }

The relevant CSS for these items is:这些项目的相关 CSS 是:

.mesh__insert-editor-container {
    margin-left: auto;
    margin-right: auto;
}

.mesh__insert-editor-video-container {
    position: relative;
    width: 100%:
}

And the HTML:和 HTML:

<section class="mesh__insert-editor-container flex__one flex-container flex-column horizontally-left-aligned" id="video-main-container">
    <div class="mesh__insert-editor-video-container flex-container horizontally-right-aligned flex-wrap">
        <video class="mesh__insert-editor-video-placeholder"></video>
    </div>
</section>

All this code is:所有这些代码是:

  • Get the height of all the elements on the page, sum them and calculate the main section height by subtracting that height;获取页面上所有元素的高度,将它们相加并通过减去该高度来计算主要部分的高度;
  • If the video height gets bigger than the section height, I reduce its height by -1px each time the window gets resized, and calculate the new width.如果视频高度大于部分高度,每次调整窗口大小时我都会将其高度减少-1px ,并计算新的宽度。

All the above code is giving me this result , which works great for most scenarios, but I need the video to size up when the condition on the if statement is not met.上面的所有代码都给了我这个结果,这在大多数情况下都很好用,但是当不满足if语句的条件时,我需要调整视频的大小。 Everything I tried inside the else statement gets "jumpy".我在else语句中尝试的所有else变得“跳跃”。

Any better alternatives to solve this would be much appreciated.任何更好的替代方案来解决这个问题将不胜感激。 Thanks all!谢谢大家!

The CSS aspect ratio trick might be a good solution: https://css-tricks.com/aspect-ratio-boxes/ CSS 纵横比技巧可能是一个很好的解决方案: https : //css-tricks.com/aspect-ratio-boxes/

The approach takes advantage of a quirk in CSS where padding based on a percentage value will be relative to the element's width.该方法利用了 CSS 中的一个怪癖,其中基于百分比值的padding将相对于元素的宽度。 Create a container using this trick, the important bit is this line:使用这个技巧创建一个容器,重要的是这一行:

padding-top: calc(9/16 * 100%);

The value is calculating the correct height based on the aspect ratio you want (9 tall over 16 wide in this case) and generating it relative to the width of the element by multiplying by 100% .该值是根据您想要的纵横比计算正确的高度(在这种情况下 9 高超过 16 宽)并通过乘以100%相对于元素的宽度生成它。

With the container maintaining aspect ratio, just place the content inside an absolute positioned inner div and you should be good.在容器保持纵横比的情况下,只需将内容放在绝对定位的内部 div 中,你应该会很好。 This solution is fully responsive at that point.该解决方案在这一点上是完全响应的。

 * { box-sizing: border-box } .outer-max-width { max-width: 960px; margin: 0 auto; } .aspect-ratio-box { width: 100%; padding-top: calc(9/16 * 100%); position: relative; border: 2px solid red; /* for demo visibility, remove */ } .aspect-ratio-box-content { position: absolute; width: 100%; top: 0; left: 0; border: 2px solid blue; /* for demo visibility, remove */ } .video-placeholder { display: block; width: 100%; height: auto; }
 <div class="outer-max-width"> <div class="aspect-ratio-box"> <div class="aspect-ratio-box-content"> <img class="video-placeholder" src="https://placeimg.com/640/360/nature" /> </div> </div> </div>

Got it to work!得到它的工作! I used this amazing CSS-only solution: https://codepen.io/EightArmsHQ/pen/BvNzrm similar to BugsArePeopleToo's suggestion, from eightarmshq:我使用了这个惊人的纯 CSS 解决方案: https ://codepen.io/EightArmsHQ/pen/BvNzrm 类似于 BugsArePeopleToo 的建议,来自八臂:

.content{
  position: absolute;
  top: 0;
  bottom: 0;
  width: 100%;
  height: 100%;

  background: #555;
  box-shadow: inset 1vh 1vh 10vh 0 rgba(0,0,0,0.5);
  display: flex;


  box-sizing: border-box;
  border: 25px solid #cecece;
}

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

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