【问题标题】:Embed youtube videos that play in fullscreen automatically嵌入自动全屏播放的 youtube 视频
【发布时间】:2014-04-21 05:05:24
【问题描述】:

所以我想做的是在我的网站上播放全屏视频。但我想自动播放 youtube 视频并自动全屏(浏览器窗口的大小)。我的网站导航是从一页滑到另一页的左右箭头。然后向上和向下滚动每个页面的向上和向下箭头。

但我唯一想做的就是全屏自动播放 youtube 视频,同样是浏览器窗口的大小。还是我必须自己主持视频?这可能更容易,但会占用我必须支付的带宽。无论如何,提前感谢您的帮助,干杯!

【问题讨论】:

    标签: javascript html video youtube fullscreen


    【解决方案1】:

    这里的回答很好:How to make a YouTube embedded video a full page width one?

    如果您在嵌入代码中的 url 末尾添加 '?rel=0&autoplay=1'(像这样)

    <iframe id="video" src="//www.youtube.com/embed/5iiPC-VGFLU?rel=0&autoplay=1" frameborder="0" allowfullscreen></iframe>
    

    它应该在加载时播放的视频。这是jsfiddle 的演示。

    【讨论】:

      【解决方案2】:

      Chrome 不支持自动全屏,但您可以像这样简单地播放视频:

      &lt;iframe width="640" height="360" src="youryoutubeurlhere" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen&gt;&lt;/iframe&gt;

      【讨论】:

        【解决方案3】:

        这将有助于autoplay 视频加载并使其全屏显示,但由于 Chrome 自动播放政策,正在运行的视频必须静音。

        // https://jsfiddle.net/jlam55555/o5njka95/6/
        
        function requestFullScreen(element) {
            // Supports most browsers and their versions.
            var requestMethod = element.requestFullScreen || element.webkitRequestFullScreen || element.mozRequestFullScreen || element.msRequestFullscreen;
        
            if (requestMethod) { // Native full screen.
                requestMethod.call(element);
            } else if (typeof window.ActiveXObject !== "undefined") { // Older IE.
                var wscript = new ActiveXObject("WScript.Shell");
                if (wscript !== null) {
                    wscript.SendKeys("{F11}");
                }
            }
        }
        // FullScreen function
        function makeFullScreen() {
            document.getElementsByTagName("iframe")[0].className = "fullScreen";
            var elem = document.body;
            requestFullScreen(elem);
        }
        iframe.fullScreen {
            width: 100%;
            height: 100%;
            position: absolute;
            top: 0;
            left: 0;
        }
        <body onload="makeFullScreen()">
        <iframe src="https://www.youtube.com/embed/668nUCeBHyY?autoplay=1&mute=1&controls=0&rel=0" frameborder="0" allow="autoplay; picture-in-picture" title="YouTube Embed"></iframe>
        </body>

        【讨论】:

          【解决方案4】:

          我找到了 2 个解决方案 ✌ 在 HTML 中嵌入 YouTube 视频

          1. 只有 HTML 没有 JS
            在这个解决方案中,我们在 iframe 参数中设置了 player 选项

              
                  body {
                      overflow-x: hidden;
                  }
          
          
          
                  .video-container {
                      width: 100vw;
                      height: 100vh;
                      overflow: hidden;
                      position: relative;
                  }
          
          
          
                  iframe {
                      position: absolute;
                      top: -10%;
                      width: 100vw;
                      height: 117vh;
                      pointer-events: none;
                  }
              
          <!DOCTYPE html>
          <html lang="en">
          
          <head>
              <meta charset="UTF-8" />
              <meta http-equiv="X-UA-Compatible" content="IE=edge" />
              <meta name="viewport" content="width=device-width, initial-scale=1.0" />
              <title>Youtube Html</title>
          
          </head>
          
          <body>
              <div class="video-container">
          
                  
                  <iframe
                      src="https://www.youtube.com/embed/rUWxSEwctFU?mute=1&modestbranding=0&autoplay=1&autohide=1&rel=0&showinfo=0&controls=0&disablekb=1&enablejsapi=1&iv_load_policy=3&loop=1&playsinline=1&fs=0&playlist=rUWxSEwctFU"></iframe>
              </div>
          </body>
          
          </html>
          1. 带 JS(首选这个)? See codepen

          【讨论】: