javascript - 使用 JavaScript 调整 HTML 多个全宽视频的大小

标签 javascript html video

我知道可以使用 CSS 在页面上制作全宽视频,但我使用 Javascript 库来显示视频,因为它在我使用的模板中具有一些漂亮的皮肤和其他功能。它非常适合单个视频,但模板将调整大小硬编码为 ID 标签,我正在努力让它与具有不同 ID 标签的多个不同视频一起使用。第二个容器不调整大小。这是代码:

    <section class="wrapper">
        <section class="full-width">
            <video id="video1" controls poster="img/poster.jpg" >
                <source src="media/sample.mp4" type="video/mp4"/>
            </video>
        </section>
    </section>

    <!-- video2 markup is almost identical to video1 --> 

    function htmlVideo() {
        videojs("video1", {
          controlBar: {
            timeDivider: false,
            fullscreenToggle: false,
            playToggle: false,
            remainingTimeDisplay: false
          },
          "height": "auto",
          "width": "auto"
        }).ready(function() {
          var myPlayer = this;
          var aspectRatio = 9 / 16;
          function resizeVideoJS() {
              var width = document.getElementById(myPlayer.id()).parentElement.offsetWidth;
              myPlayer.width(width).height(width * aspectRatio);
              // I added these two lines but they don't work
              var width2 = document.getElementById("video2").parentElement.offsetWidth;
              document.getElementById("video2").width(width2).height(width2 * aspectRatio);
          }
          resizeVideoJS();
          window.onresize = resizeVideoJS;
        });

      // I added the following code as well trying to initialize the
      // video, but it doesn't work
      videojs("video2", {
        controlBar: {
        timeDivider: false,
        fullscreenToggle: false,
        playToggle: false,
        remainingTimeDisplay: false
      },
      "height": "auto",
      "width": "auto"
      }).ready(function() {
        resizeVideoJS();
    });
 }

最佳答案

确保每个播放器实例引用一个player对象(由库提供),而不是直接引用video元素dom节点。

关于javascript - 使用 JavaScript 调整 HTML 多个全宽视频的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33828875/

相关文章:

javascript - 如何在 JavaScript 中编写 import 语句而不是 require 语句?

通过 HTML5 视频元素的 Java 多媒体流

javascript - 使用 Facebook 等模式确认离开页面的导航?

javascript - 使用 Javascript 和 Canvas 重复绘图

javascript - 鼠标移开时启动计时器 鼠标悬停时启动计时器 javascript

python - Tkinter 中的视频输出?

javascript - 在 JavaScript 中使用/定义函数时出错

javascript - 在高延迟网络下使用 Ember Data 时出现奇怪的行为

javascript - jQuery:如何将 div 内的串联元素与 div 的其余部分分开设置样式?

javascript - 如何将图像和视频交换到另一个 div?