javascript - 为什么我的 YouTube 嵌入拒绝在视频轮播中调整大小?

标签 javascript jquery css twitter-bootstrap carousel

我正在学习网络开发并尝试构建网站。我正在尝试使用 YouTube 视频制作旋转木马。我所拥有的正在运行,但它拒绝根据我的需要调整大小。

这就是我得到的。宽但短的视频尺寸。 This is what I'm getting. A wide but short video size.

但这就是我想要的,最接近全屏,但我想保留轮播中的左/右导航并让它们出现在视频内容的顶部。 enter image description here

这是我的代码:请看一下:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <style>
  .carousel-inner > .item > iframe,
  .carousel-inner > .item > a > img {
      width: 100%;
      margin: auto;
  }
  </style>
</head>
<body style="height:100%;">

<div style="height:100%;">
  <div id="myCarousel" class="carousel slide" data-ride="carousel" style="height:100%;">
    <!-- Indicators -->
    <ol class="carousel-indicators">
      <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
      <li data-target="#myCarousel" data-slide-to="1"></li>
      <li data-target="#myCarousel" data-slide-to="2"></li>
      <li data-target="#myCarousel" data-slide-to="3"></li>
    </ol>

    <!-- Wrapper for slides -->
    <div class="carousel-inner" role="listbox" style="height:100%;">

      <div class="item active" style="height:100%;">
        <iframe width="100%" height="100%" src="https://www.youtube.com/embed/UTt33udwRw0?autoplay=1" frameborder="0" allowfullscreen></iframe>
      </div>

      <div class="item" style="height:100%;">
        <iframe width="100%" height="100%" src="https://www.youtube.com/embed/UTt33udwRw0?autoplay=1" frameborder="0" allowfullscreen></iframe>
      </div>

      <div class="item" style="height:100%;">
        <iframe width="100%" height="100%" src="https://www.youtube.com/embed/UTt33udwRw0?autoplay=1" frameborder="0" allowfullscreen></iframe>
      </div>

      <div class="item" style="height:100%;">
        <iframe width="100%" height="100%" src="https://www.youtube.com/embed/UTt33udwRw0?autoplay=1" frameborder="0" allowfullscreen></iframe>
      </div>

    </div>
    <!-- Left and right controls -->
    <a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
      <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
      <span class="sr-only">Previous</span>
    </a>
    <a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
      <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
      <span class="sr-only">Next</span>
    </a>
  </div>
</div>

</body>
</html>

最佳答案

height:100% 是相对于父元素的。在这种情况下,html 是父元素并且没有定义的高度。如果将 height:100%; 添加到 html 标签的 CSS 中,它应该可以工作:

<!DOCTYPE html>
<html lang="en" style="height:100%;">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <style>
  .carousel-inner > .item > iframe,
  .carousel-inner > .item > a > img {
      width: 100%;
      margin: auto;
  }
  </style>
</head>
<body style="height:100%;">

<div style="height:100%;">
  <div id="myCarousel" class="carousel slide" data-ride="carousel" style="height:100%;">
    <!-- Indicators -->
    <ol class="carousel-indicators">
      <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
      <li data-target="#myCarousel" data-slide-to="1"></li>
      <li data-target="#myCarousel" data-slide-to="2"></li>
      <li data-target="#myCarousel" data-slide-to="3"></li>
    </ol>

    <!-- Wrapper for slides -->
    <div class="carousel-inner" role="listbox" style="height:100%;">

      <div class="item active" style="height:100%;">
        <iframe width="100%" height="100%" src="https://www.youtube.com/embed/UTt33udwRw0?autoplay=1" frameborder="0" allowfullscreen></iframe>
      </div>

      <div class="item" style="height:100%;">
        <iframe width="100%" height="100%" src="https://www.youtube.com/embed/UTt33udwRw0?autoplay=1" frameborder="0" allowfullscreen></iframe>
      </div>

      <div class="item" style="height:100%;">
        <iframe width="100%" height="100%" src="https://www.youtube.com/embed/UTt33udwRw0?autoplay=1" frameborder="0" allowfullscreen></iframe>
      </div>

      <div class="item" style="height:100%;">
        <iframe width="100%" height="100%" src="https://www.youtube.com/embed/UTt33udwRw0?autoplay=1" frameborder="0" allowfullscreen></iframe>
      </div>

    </div>
    <!-- Left and right controls -->
    <a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
      <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
      <span class="sr-only">Previous</span>
    </a>
    <a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
      <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
      <span class="sr-only">Next</span>
    </a>
  </div>
</div>

</body>
</html>

我还建议使用 style 标签或外部样式表而不是内联 CSS,因为它更易于管理。

关于javascript - 为什么我的 YouTube 嵌入拒绝在视频轮播中调整大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40598037/

相关文章:

javascript - 如何使用 jQuery 替换输入字段的特定值?

css - yiistrap TbNav 按钮样式

javascript - 使用 HTTPS 时不显示上传进度

javascript - 从导航栏到同一页面内主要部分的链接(内部 php/html 文件)

jquery - 如何在选择选项中获取数据属性和值作为对象(使用所选插件)

javascript - jQuery CSS 样式仅适用于 iOS 上的第一个元素

javascript - 从 HTML 页面上的另一个元素继承宽度

javascript - Jquery 嵌套函数不起作用

javascript - 有没有办法使用 h.264 编解码器恢复实时 h264 播放(客户端重新加入)?

javascript - knockout 循环中的嵌套绑定(bind)