我正在开发一个视频编辑工具,需要在水平和垂直方向调整屏幕大小时保持视频的
这些项目的相关CSS为:
16:9
宽高比。到目前为止,当水平调整大小和垂直向下调整大小时,我已经按预期使其正常工作,但是在 垂直向上调整大小 时无法使其正常工作。我使用的 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
语句内尝试的所有内容都会“跳动”。
如有更好的解决方案,将不胜感激。谢谢大家!
mainSection
进行所有计算,而不是当前视频高度(和宽度)。也就是说,根据mainSection
的宽度和高度进行计算。 - GetSetmainSection
比视频本身要高,所以也会导致出错。 - Juan Martín García