在YouTube视频开始之前,请显示自定义图片



我正在尝试在"播放器"div中显示图像,然后在访问者单击按钮后,用视频替换(用iframe替换DIV)。这是我的代码:

<!DOCTYPE html> 
<head>
<style type="text/css">
html {
text-align: center;
}
#player {
display: inline-block;
width: 640px;
height: 360px;
background: url(http://placehold.it/640x360) no-repeat;
}
</style>
</head>
<html>
  <body>
    <!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
    <div id="player"></div>
    <p><button onclick="playMe()">Play</button></p>
    <script>
    function playMe() {
      // 2. This code loads the IFrame Player API code asynchronously.
      var tag = document.createElement('script');
      tag.src = "https://www.youtube.com/iframe_api";
      var firstScriptTag = document.getElementsByTagName('script')[0];
      firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
      // 3. This function creates an <iframe> (and YouTube player)
      //    after the API code downloads.
      var player;
      function onYouTubeIframeAPIReady() {
        player = new YT.Player('player', {
          height: '360',
          width: '640',
          videoId: 'JW5meKfy3fY',
          playerVars: { 'autoplay': 0, 'controls': 0, 'rel': 0, 'showinfo': 0 },
          events: {
            'onReady': onPlayerReady
          }
        });
      }
      // 4. The API will call this function when the video player is ready.
      function onPlayerReady(event) {
        event.target.playVideo();
      }
    }
    </script>
  </body>
</html>

但行不通。

删除" Playme"功能时,它可以工作,但是在单击按钮/DIV/链接后,我需要视频启动。我试图将图片和视频放入单独的 div s,在视频顶部和点击之后隐藏顶部div并启动视频,但这也不起作用。

这是一个可变范围问题。

功能Onyoutubeiframeapready需要是一个全局变量 - 播放器变量也是如此。

我相信这个示例应该作为您想要的基础(如果您在API下载之前单击按钮,则有种族条件,但是完整的实现可能是特定于应用程序的读者)

    <!DOCTYPE html> 
<head>
<style type="text/css">
html {
text-align: center;
}
#player {
display: inline-block;
width: 640px;
height: 360px;
background: url(http://placehold.it/640x360) no-repeat;
}
</style>
</head>
<html>
  <body>
    <!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
    <div id="player"></div>
    <p><button onclick="playMe()">Play</button></p>
    <script>
    // 2. This code loads the IFrame Player API code asynchronously.
    var tag = document.createElement('script');
    tag.src = "https://www.youtube.com/iframe_api";
    var firstScriptTag = document.getElementsByTagName('script')[0];
    firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
    // 3. called after the API code downloads.
    var player;
    function onYouTubeIframeAPIReady() {
        // don't need anything here
    }
    // 4. The API will call this function when the video player is ready.
    function onPlayerReady(event) {
        event.target.playVideo();
    }
    function playMe() {
        if (window.YT) {
            player = new YT.Player('player', {
                        height: '360',
                        width: '640',
                        videoId: 'JW5meKfy3fY',
                        playerVars: { 'autoplay': 0, 'controls': 0, 'rel': 0, 'showinfo': 0 },
                        events: {
                            'onReady': onPlayerReady
                        }
                    });
        }
    }
    </script>
  </body>
</html>

相关内容

  • 没有找到相关文章

最新更新