【问题标题】:embed youtube video responsive solution嵌入 youtube 视频响应解决方案
【发布时间】:2016-07-23 09:24:02
【问题描述】:

谁能帮帮我:

我试图制作一个 100% 宽度和响应式的嵌入 youtube 视频, 我在这个网站上找到了这段代码来尝试完成这个:

http://avexdesigns.com/responsive-youtube-embed/

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%;
}

HTML:

<div class="video-container">
     <iframe src="http://www.youtube.com/embed/dFVxGRekRSg" frameborder="0" width="560" height="315"></iframe>
</div>

使用原样的代码:视频甚至没有出现: 所以我将溢出隐藏在代码中并将“padding-top”更改为“top”:将视频向下推:视频在我的页面顶部,它应该在上面写着“为什么 kevin Kurbs 室内设计”的下方/看下面的视频?”

为了部分获得我希望它显示的视频,我从我的 html 中删除并相应地调整 iframe:

iframe { 
    width: 100%;
    top: 5px;  
    margin-bottom: -5px
}

视频代码为288-304

http://graph-art.matc.edu/harrisd5/vicom126/a2/index.html

这样做只是让 youtube 缩略图封面照片在播放视频时做出响应,它是中心。那么我如何制作这个视频 100% 宽度和响应,同时保持其纵横比放置在“为什么是 Kevin Kurbs 内部栏?”下方。 ?

【问题讨论】:

    标签: html css video youtube


    【解决方案1】:

    在这种情况下,您可以为 iframe 使用计算出的高度。它是根据 youtube 视频 iframe 的页面宽度和默认纵横比计算得出的。

    iframe {
        width: 100%;
        top: 5px;
        margin-bottom: -5px;
        height: calc(100vw*(315/560));
    }
    

    【讨论】:

      【解决方案2】:

      当我之前完成响应式视频时,我使用了这个 jQuery。 调整屏幕大小时,它会计算高度。

      <script>    
      var $allVideos = $("iframe[src^='https://player.vimeo.com'], iframe[src^='http://player.vimeo.com'], iframe[src^='https://www.youtube.com'], iframe[src^='http://www.youtube.com'], object, embed"),
      
      
          $allVideos.each(function() {
      
            $(this)
              // jQuery .data does not work on object/embed elements
              .attr('data-aspectRatio', this.height / this.width)
              .removeAttr('height')
              .removeAttr('width');
      
          });
      
          $(window).resize(function() {
      
      
      
            $allVideos.each(function() {
      
              $fluidEl = $(this).parent();
              var newWidth = $fluidEl.width();
      
              var $el = $(this);
              $el
                  .width(newWidth)
                  .height(newWidth * $el.attr('data-aspectRatio'));
      
            });
      
          }).resize();
      </script>
      

      【讨论】:

        【解决方案3】:

        这是 CSS 代码:

        .yt-container {
          position:relative;
          padding-bottom:56.25%;
          padding-top:30px;
          height:0;
          overflow:hidden;
        }
        .yt-container iframe, .yt-container object, .yt-container embed {
          position:absolute;
          top:0;
          left:0;
          width:100%;
          height:100%;
        }
        

        下面是HTML代码:

        <div class="yt-container">
           <iframe src="https://www.youtube.com/embed/hfQdkBOxXTc" frameborder="0" allowfullscreen></iframe>
        </div>
        

        来源:Make YouTube Embed Video Responsive Using CSS

        【讨论】:

          【解决方案4】:

          这是我找到的解决方案:

          HTML:

          <div class='embed-container'>
              <iframe src='https://www.youtube.com/embed/QILiHiTD3uc' frameborder='0' allowfullscreen></iframe>
          </div>
          

          CSS:

          .embed-container {
              position: relative;
              padding-bottom: 56.25%;
              height: 0;
              overflow: hidden;
              max-width: 100%;
          }
          
          .embed-container iframe, .embed-container object, .embed-container embed {
              position: absolute;
              top: 0;
              left: 0;
              width: 100%;
              height: 100%;
          }
          

          【讨论】:

            猜你喜欢
            • 2012-01-15
            • 1970-01-01
            • 1970-01-01
            • 2015-10-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多