【发布时间】:2020-01-31 01:54:04
【问题描述】:
我正在开发视频编辑工具,在水平和垂直调整屏幕大小时,我需要保持视频的16:9 纵横比。到目前为止,我在水平调整大小和垂直调整大小时都能按预期工作,但在sizing up vertically 时无法正常工作。我用来计算视频高度和调整大小的 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
}
}
这些项目的相关 CSS 是:
.mesh__insert-editor-container {
margin-left: auto;
margin-right: auto;
}
.mesh__insert-editor-video-container {
position: relative;
width: 100%:
}
还有 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>
所有这些代码是:
- 获取页面上所有元素的高度,将它们相加并减去该高度来计算主要部分的高度;
- 如果视频高度大于部分高度,我会在每次调整窗口大小时将其高度减小
-1px,然后计算新宽度。
以上所有代码都给了我this result,这在大多数情况下都很好用,但是当if 语句的条件不满足时,我需要调整视频的大小。我在else 语句中尝试的所有内容都变得“跳跃”。
任何更好的解决方案将不胜感激。谢谢大家!
【问题讨论】:
-
I reduce its height by -1px... 这在您慢慢地调整大小时有效。如果您以正常速度调整窗口大小,它会起作用吗? ...但要提供改进建议,请考虑根据您的mainSection进行所有计算,而不是目前的视频高度(和宽度)。也就是根据mainSection的宽度和高度进行计算。 -
感谢您的回复!我也试过了,但是
mainSection比视频本身高,所以它也会把事情扔掉
标签: javascript css reactjs video aspect-ratio