简体   繁体   English

HTML5 视频适合宽度或高度

[英]HTML5 video fit width OR height

The width=100% height="auto" scales my videos to fit the whole width of the browser window, but if the height of the window is lower than the aspect ratio the video gets cropped in height. width=100% height="auto"缩放我的视频以适应浏览器 window 的整个宽度,但如果 window 的高度低于纵横比,则视频的高度会被裁剪。

I want the video to scale to fit either width or height so that I can always see the whole video without crop, filling the closest ratio (adding empty space to sides if window ratio is lower).我希望视频可以缩放以适应宽度或高度,这样我就可以始终看到没有裁剪的整个视频,填充最接近的比例(如果 window 比例较低,则在两侧添加空白区域)。

I can't figure out how to do this however.但是我不知道该怎么做。
First any height value, % or px, seems to be disregarded, I can't set the size at all using height, only width.首先,任何高度值(% 或 px)似乎都被忽略了,我根本无法使用高度设置尺寸,只能使用宽度。 Same for min-width/height.最小宽度/高度相同。

How can this be done?如何才能做到这一点?

My code:我的代码:

<video id="video" width=100% height="auto" onclick=playPause()></video>

Video is loaded by javascript where v is a video link from an array:视频由 javascript 加载,其中 v 是来自数组的视频链接:

video.addEventListener('play', function() { v.play();}, false);

Try wrapping the value inside quotes尝试将值括在引号内

<div class="videoContainer">
   <video id="video" width="100%" height="auto" onclick="playPause();"></video>
</div>

you can add these class你可以添加这些类

.videoContainer
{
   position:absolute;
   height:100%;
   width:100%;
    overflow: hidden;
}

.videoContainer video
{
  min-width: 100%;
  min-height: 100%;
}

Or alternatively use this或者或者使用这个

video {
  object-fit: cover;
}

Solved it myself with a javascript function:自己用javascript函数解决了这个问题:

window.addEventListener('resize', resize, false);

video.height = 100; /* to get an initial width to work with*/
resize();

function resize() {
videoRatio = video.height / video.width;
windowRatio = window.innerHeight / window.innerWidth; /* browser size */

    if (windowRatio < videoRatio) {
        if (window.innerHeight > 50) { /* smallest video height */
                video.height = window.innerHeight;
        } else {
            video.height = 50;
    }
    } else {
        video.width = window.innerWidth;
    }

};

There's a great auto width/height trick called intrinsic ratios that can be used on images/videos and the likes!有一个很棒的自动宽度/高度技巧,称为固有比率,可用于图像/视频等! The trick is to have your desired element in a wrapper that then has a percentage-valued padding applied to it.诀窍是将所需的元素放在包装器中,然后对其应用百分比值填充

Your markup would then be:您的标记将是:

<div class="video-wrapper">
  <video class="video" id="video" onclick=playPause()></video>
</div>

And your CSS:还有你的 CSS:

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

.video {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: #ccc; /* Random BG colour for unloaded video elements */
}

You can limit the dimensions of your video container by playing with min-width or max-width of the .video-wrapper styles.您可以通过播放.video-wrapper样式的min-widthmax-width来限制视频容器的尺寸。

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

I had a similar problem.我有一个类似的问题。 I have made a `slideshow' with various objects as slides, some of them are videos of various sizes.我制作了一个“幻灯片”,其中包含各种对象作为幻灯片,其中一些是各种大小的视频。 I did not found any conceptual solution, so I have found a trick.我没有找到任何概念上的解决方案,所以我找到了一个技巧。

I have explicitely specified all real video sizes in tags and collected the information of all of them:我已经在标签中明确指定了所有真实的视频大小并收集了所有这些的信息:

var vids = document.getElementsByClassName("vid"); // videos
var vidl = vids.length;
var vidH = [ ];
var vidW = [ ];

// Get original widths and heights of videos
for (i=0; i<vidl; i++) { // >
    vidH.push(vids[i].height);
    vidW.push(vids[i].width);
}

Then (when necessary) I define the window size as follows:然后(必要时)我定义窗口大小如下:

// Current inner height of the browser window (in pixels)

var iH = window.innerHeight
         || document.documentElement.clientHeight
         || document.body.clientHeight;
// Current inner width of the browser window (in pixels)
var iW = window.innerWidth
         || document.documentElement.clientWidth
         || document.body.clientWidth;

To rescale a rectangle into another rectangle we have to find a minimal scale:要将一个矩形重新缩放为另一个矩形,我们必须找到一个最小比例:

var r, rh, rw;

// Scale videos to fit window
for (i=0; i<vidl; i++) { // >
    rh=iH / vidH[i]; rw=iW / vidW[i]; r=Math.min(rh, rw);
    if (rh>rw) {
       vids[i].height=Math.floor(vidH[i]*r);
       vids[i].width=iW;
    }
    else {
       vids[i].height=iH-5;
       vids[i].width=Math.floor(vidW[i]*r);
    }
}

Unfotunately, here appeared some extra space;不幸的是,这里出现了一些额外的空间; in FireFox I have found its minimal value as 5 that I negate form windows height value (iH) as the new video size.在 FireFox 中,我发现它的最小值为 5,我将窗体窗口高度值 (iH) 否定为新的视频大小。

Duplicate issue on the height posted at HTML5 Video dimensions in Safari IOS 在 Safari IOS 中HTML5 视频尺寸发布的高度上的重复问题

Pure CSS solution provided in: https://stackoverflow.com/a/60878701/2486998纯 CSS 解决方案提供于: https : //stackoverflow.com/a/60878701/2486998

You can add style:您可以添加样式:

object-fit: contain;

In Angular if video player (shaka player in my case) is a separate component, this works perfectly (either height or width of container will cause resize of the player)在 Angular 中,如果视频播放器(在我的例子中是 shaka 播放器)是一个单独的组件,那么它可以完美地工作(容器的高度或宽度都会导致播放器的大小调整)

HTML HTML

 <div id="videoContainer"        
      class="videoContainer">
   <video id="videoPlayer"></video>
 </div>

CSS CSS

.videoContainer
{
   height:100%;
   width:100%;
}

.videoContainer video
{
  width: 100%;
  height: auto;
  max-width: 100%;
  max-height: 100%;
}

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

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