响应式全屏YouTube视频,没有黑条?

Owl*_*wly 22 html css youtube video

我在我的网站上嵌入了全屏YouTube视频.

在此输入图像描述

当浏览器的大小与视频的宽度和高度成比例时,它看起来很好.但是,当我将浏览器的大小调整为不同的比例时,我会在视频周围看到黑条.

在此输入图像描述

我想要的是让视频始终填满整个窗口,但没有任何拉伸.当浏览器大小与视频不成比例时,我希望隐藏"多余".

我想要实现的是你可以在这两个网站的背景中看到的:http://ginlane.com/http://www.variousways.com/.

是否可以使用YouTube视频执行此操作?

Mik*_*ike 10

这已经很老了,但人们可能仍然需要帮助.我也需要这个,所以创建了我的解决方案的笔,应该有帮助 - http://codepen.io/MikeMooreDev/pen/QEEPMv

该示例显示了同一视频的两个版本,一个是标准的,第二个是裁剪的,并且居中对齐以适应完整父元素的大小而不显示可怕的黑条.

您需要使用一些javascript来计算视频的新宽度,但如果您知道宽高比,则非常容易.

HTML

<div id="videoWithNoJs" class="videoWrapper">
  <iframe src="https://www.youtube.com/embed/ja8pA2B0RR4" frameborder="0" allowfullscreen></iframe>
</div>
Run Code Online (Sandbox Code Playgroud)

CSS - videoWrapper的高度和宽度可以是任何东西,如果你愿意,它们可以是百分比

.videoWrapper {
  height:600px;
  width:600px;
  position:relative;
  overflow:hidden;
}

.videoWrapper iframe {
    height:100%;
    width:100%;
    position:absolute;
    top:0;
    bottom:0;
}
Run Code Online (Sandbox Code Playgroud)

jQuery的

$(document).ready(function(){
    // - 1.78 is the aspect ratio of the video
    // - This will work if your video is 1920 x 1080
    // - To find this value divide the video's native width by the height eg 1920/1080 = 1.78
    var aspectRatio = 1.78;

    var video = $('#videoWithJs iframe');
    var videoHeight = video.outerHeight();
    var newWidth = videoHeight*aspectRatio;
    var halfNewWidth = newWidth/2;

    video.css({"width":newWidth+"px","left":"50%","margin-left":"-"+halfNewWidth+"px"});

});
Run Code Online (Sandbox Code Playgroud)

这也可以在调整大小时触发,以确保它保持响应.最简单(可能不是最有效)的方法是使用以下方法.

$(window).on('resize', function() {

    // Same code as on load        
    var aspectRatio = 1.78;
    var video = $('#videoWithJs iframe');
    var videoHeight = video.outerHeight();
    var newWidth = videoHeight*aspectRatio;
    var halfNewWidth = newWidth/2;

    video.css({"width":newWidth+"px","left":"50%","margin-left":"-"+halfNewWidth+"px"});

});
Run Code Online (Sandbox Code Playgroud)


Jit*_*n C 8

围绕iframe代码创建一个容器div,并给它一个类,例如:

<div class="video-container"><iframe.......></iframe></div>
Run Code Online (Sandbox Code Playgroud)

添加CSS:

.video-container {
    position:relative;
    padding-bottom:56.25%;
    padding-top:30px;
    height:0;
    overflow:hidden;
}

.video-container iframe, .video-container object, .video-container embed {
    position:absolute;
    top:0;
    left:0;
    width:100%;
    height:100%;
}
Run Code Online (Sandbox Code Playgroud)

这个coolestguidestheplanet.com是这个答案的源URL.

  • 这对我有用,只有我必须删除的是填充顶部 (2认同)

kus*_*ikh 5

为了模拟相同的效果,重要的是保持纵横比为16:9。

的HTML

<div class="banner-video">    
        <iframe  src="https://www.youtube.com/embed/XXlJXRXJhow?rel=0&amp;controls=0&amp;showinfo=0&amp;autoplay=1&amp;mute=1&amp;loop=1&amp;playlist=XXlJXRXJhow" frameborder="0" allow="autoplay; encrypted-media" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe> 
</div>
Run Code Online (Sandbox Code Playgroud)

的CSS

iframe{
  width: 100%;
  height: calc((100vw*9) /16);
}
Run Code Online (Sandbox Code Playgroud)

这将删除黑条。

现在,我们可以将外部容器设置为100%的宽度和100%的高度,并隐藏溢出。

.banner-video{
  height: 100vh;
  overflow: hidden;
}
Run Code Online (Sandbox Code Playgroud)

现在,上面的代码将起作用,直到视口高宽比大于16/9。因此,我们必须根据长宽比添加媒体查询。

 @media (max-aspect-ratio: 16/9) {
    .banner-video{
      width: 100%;
      overflow: hidden;
    }

    iframe{
      width: calc((100vh*16)/9);
      height: 100vh;
      }
  }
Run Code Online (Sandbox Code Playgroud)

之后,此youtube视频将在所有情况下保持完整的视口大小,并隐藏视频的其余部分。除此以外,所有浏览器均支持它。