首页 文章

使用HTML5 CSS3强制iframe YouTube视频居中,并在后台完全覆盖屏幕

提问于
浏览
9

你如何强制HTML5 iframe YouTube视频居中,最终使用CSS3 HTML覆盖全屏窗口背景?

例如“paypal.it " home page background or " unity3d.com/5”顶级视频,有iframe youtube视频 . iframe 覆盖全屏(缩放)并在重新调整窗口大小时覆盖所有宽度和高度 . 它重新调整大小,保持100%最小宽度缩放高度或100%最小高度缩放宽度 .

如何使用 iframe HTML5和CSS3实现此效果?

代码示例HTML5

<div class="video" style="opacity: 1;">

    <iframe src="http://www.youtube.com/embed/AddHereVideoId?autoplay=1&amp;html5=1" frameborder="0" style="height: 720px;">
     </iframe>

</div>

代码CSS3 HTML最终将获得Java帮助 .

2 回答

  • 8

    对于真正的全屏解决方案,可以这样实现:

    HTML

    <div class="video-background">
        <div class="video-foreground">
          <iframe src="https://www.youtube.com/embed/I4agXcHLySs?controls=0&showinfo=0&rel=0&autoplay=1&loop=1&mute=1" frameborder="0" allowfullscreen></iframe>
        </div>
      </div>
    

    CSS

    * { box-sizing: border-box; }
    .video-background {
      background: #000;
      position: fixed;
      top: 0; right: 0; bottom: 0; left: 0;
      z-index: -99;
    }
    .video-foreground,
    .video-background iframe {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      pointer-events: none;
    }
    
    @media (min-aspect-ratio: 16/9) {
      .video-foreground { height: 300%; top: -100%; }
    }
    @media (max-aspect-ratio: 16/9) {
      .video-foreground { width: 300%; left: -100%; }
    }
    @media all and (max-width: 600px) {
    .vid-info { width: 50%; padding: .5rem; }
    .vid-info h1 { margin-bottom: .2rem; }
    }
    @media all and (max-width: 500px) {
    .vid-info .acronym { display: none; }
    }
    

    它并不完美,例如它不适用于容器的极高纵横比,但在大多数情况下做得很好 . 这是一个工作示例:https://codepen.io/anon/pen/zRVLGJ

  • 3

    我认为这是你想要实现的目标:

    .video-wrapper {position: relative; padding-bottom: 56.25%; /* 16:9 */  padding-top: 25px;}
    .video-wrapper iframe {position: absolute; top: 0; left: 0; width: 100%; height: 100%;}
    
    <div class="video-wrapper">
      <iframe src="http://www.youtube.com/embed/AddHereVideoId?autoplay=1&amp;html5=1" frameborder="0" allowfullscreen></iframe>
    </div>
    

    这将为您提供一个视频,填充它所在的容器,并自动缩放高度 .

    我最初在这里找到了这个解决方案:https://css-tricks.com/NetMag/FluidWidthVideo/Article-FluidWidthVideo.php

相关问题