• <small id='2efJN'></small><noframes id='2efJN'>

    <tfoot id='2efJN'></tfoot>

      <legend id='2efJN'><style id='2efJN'><dir id='2efJN'><q id='2efJN'></q></dir></style></legend>
      <i id='2efJN'><tr id='2efJN'><dt id='2efJN'><q id='2efJN'><span id='2efJN'><b id='2efJN'><form id='2efJN'><ins id='2efJN'></ins><ul id='2efJN'></ul><sub id='2efJN'></sub></form><legend id='2efJN'></legend><bdo id='2efJN'><pre id='2efJN'><center id='2efJN'></center></pre></bdo></b><th id='2efJN'></th></span></q></dt></tr></i><div id='2efJN'><tfoot id='2efJN'></tfoot><dl id='2efJN'><fieldset id='2efJN'></fieldset></dl></div>
      • <bdo id='2efJN'></bdo><ul id='2efJN'></ul>
      1. 当用户滚动离开时如何暂停 youtube 嵌入

        how can I pause a youtube embed when a user scrolls away(当用户滚动离开时如何暂停 youtube 嵌入)
            <tfoot id='dAW37'></tfoot>

          • <small id='dAW37'></small><noframes id='dAW37'>

                <tbody id='dAW37'></tbody>
                <bdo id='dAW37'></bdo><ul id='dAW37'></ul>
                <legend id='dAW37'><style id='dAW37'><dir id='dAW37'><q id='dAW37'></q></dir></style></legend>

                  <i id='dAW37'><tr id='dAW37'><dt id='dAW37'><q id='dAW37'><span id='dAW37'><b id='dAW37'><form id='dAW37'><ins id='dAW37'></ins><ul id='dAW37'></ul><sub id='dAW37'></sub></form><legend id='dAW37'></legend><bdo id='dAW37'><pre id='dAW37'><center id='dAW37'></center></pre></bdo></b><th id='dAW37'></th></span></q></dt></tr></i><div id='dAW37'><tfoot id='dAW37'></tfoot><dl id='dAW37'><fieldset id='dAW37'></fieldset></dl></div>

                  本文介绍了当用户滚动离开时如何暂停 youtube 嵌入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我一直在寻求帮助,让视频在用户滚动离开时暂停.我已经能够找到有关 html5 视频的帮助,但现在我还需要知道如何使用 youtube API 来做同样的事情.

                  I have been trying to look for help with respect getting a video to pause when a user scrolls away. I have already been able to find help for html5 videos but now I also need to know how the youtube API can be used for the same.

                  嵌入YouTube的html结构如下

                  the html structure I have that embeds the YouTube is as follows

                       <div class="ytube-container">
                  
                       <iframe id="vplayer" 
                        src="//www.youtube.com/embed/qKaMgJwBItM?modestbranding=1&showinfo=0&modestbranding=0&controls=1&rel=0&autoplay=1&vq=hd720" 
                      frameborder="0"webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
                  
                      </div>
                  

                  我将以下内容用于 html5 - youtube API 是否有非常不同的方法来做同样的事情?

                  I used the following for html5 - does the youtube API have a very different method to do the same?

                      <script>
                      //play when video is visible
                      var videos = document.getElementsByTagName("video"), fraction = 0.8;
                  
                      function checkScroll() {
                  
                      for(var i = 0; i < videos.length; i++) {
                      var video = videos[i];
                      var x = 0,
                      y = 0,
                      w = video.offsetWidth,
                      h = video.offsetHeight,
                      r, //right
                      b, //bottom
                      visibleX, visibleY, visible,
                      parent;
                  
                      parent = video;
                      while (parent && parent !== document.body) {
                      x += parent.offsetLeft;
                      y += parent.offsetTop;
                      parent = parent.offsetParent;
                      }
                  
                      r = x + w;
                      b = y + h;
                  
                      visibleX = Math.max(0, Math.min(w, window.pageXOffset + window.innerWidth - x, r - window.pageXOffset));
                      visibleY = Math.max(0, Math.min(h, window.pageYOffset + window.innerHeight - y, b - window.pageYOffset));
                  
                      visible = visibleX * visibleY / (w * h);
                  
                      if (visible > fraction) {
                      video.play();
                      } else {
                      video.pause();
                      }
                      }
                  
                      }
                  
                      window.addEventListener('scroll', checkScroll, false);
                      window.addEventListener('resize', checkScroll, false);
                  
                      //check at least once so you don't have to wait for scrolling for the video to start
                      window.addEventListener('load', checkScroll, false);
                      checkScroll();
                  
                      </script>  
                  

                  我不确定我是否了解如何完全使用 Youtube API如果另一个玩家正在玩,我能够找到一个停止一个玩家的代码,但我不知道如何操纵它来实现我需要的东西.

                  I am not sure I understand how to entirely use the Youtube API I was able to find a code that stops one player if the other is playing but I dont see how that can be manipulated to achieve what I need.

                  <script>
                  
                  players = new Array();
                  
                  function onYouTubeIframeAPIReady() {
                  var temp = $("iframe.vplayer");
                  for (var i = 0; i < temp.length; i++) {
                      var t = new YT.Player($(temp[i]).attr('id'), {
                          events: {
                              'onStateChange': onPlayerStateChange
                          }
                      });
                      players.push(t);
                  }
                  
                  }
                  onYouTubeIframeAPIReady();
                  
                  
                  function onPlayerStateChange(event) {
                  
                  if (event.data == YT.PlayerState.PLAYING) {
                      var temp = event.target.a.src;
                      var tempPlayers = $("iframe.yt_players");
                      for (var i = 0; i < players.length; i++) {
                          if (players[i].a.src != temp) players[i].stopVideo();
                  
                      }
                  }
                  }
                  

                  推荐答案

                  您在 HTML5 播放器和函数 checkScroll() 方面做得很好.
                  看来你在使用 YouTube JS 播放器时遇到了麻烦,如果你花时间阅读文档,你会发现 YouTube 播放器只是一个 iframe.

                  You done a great job with HTML5 player and the function checkScroll().
                  It seem you have trouble to use it with the YouTube JS Player, well if you take the time to read the doc, you discover that the YouTube player is just an iframe.

                  试试我制作的这个活生生的例子:http://jsbin.com/cocatuta/15/edit?js,输出

                  Try this live example i made : http://jsbin.com/cocatuta/15/edit?js,output

                  所以基本上我只是替换:

                  So basically i just replace :

                  var videos = document.getElementsByTagName("video"), fraction = 0.8;
                  

                  通过这个:

                  var videos = document.getElementsByTagName("iframe"), fraction = 0.8;
                  

                  并添加两个函数playVideopauseVideo.

                  function playVideo() {
                    player.playVideo();
                  }
                  
                  function pauseVideo() {
                    player.pauseVideo();
                  }
                  

                  完整代码:

                  //play when video is visible
                  var videos = document.getElementsByTagName("iframe"), fraction = 0.8;
                  
                  function checkScroll() {
                  
                  
                    for(var i = 0; i < videos.length; i++) {
                      var video = videos[i];
                  
                      var x = 0,
                          y = 0,
                          w = video.width,
                          h = video.height,
                          r, //right
                          b, //bottom 
                          visibleX, visibleY, visible,
                          parent;
                  
                  
                      parent = video;
                      while (parent && parent !== document.body) {
                        x += parent.offsetLeft;
                        y += parent.offsetTop;
                        parent = parent.offsetParent;
                      }
                  
                      r = x + parseInt(w);
                      b = y + parseInt(h);
                  
                  
                      visibleX = Math.max(0, Math.min(w, window.pageXOffset + window.innerWidth - x, r - window.pageXOffset));
                      visibleY = Math.max(0, Math.min(h, window.pageYOffset + window.innerHeight - y, b - window.pageYOffset));
                  
                  
                      visible = visibleX * visibleY / (w * h);
                  
                  
                      if (visible > fraction) {
                        playVideo();
                      } else {
                        pauseVideo()
                  
                      }
                    }
                  
                  };
                  
                  window.addEventListener('scroll', checkScroll, false);
                  window.addEventListener('resize', checkScroll, false);
                  
                  //check at least once so you don't have to wait for scrolling for the video to start
                  window.addEventListener('load', checkScroll, false);
                  checkScroll();
                  

                  希望对您有所帮助!是的,阅读文档甚至应该帮助你"(最终)

                  Hope it's help ! And yes, read the doc "is even supposed to help you" (eventually)

                  这篇关于当用户滚动离开时如何暂停 youtube 嵌入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Pause youtube video, youtube api(暂停 youtube 视频,youtube api)
                  Youtube iframe api not triggering onYouTubeIframeAPIReady(Youtube iframe api 未触发 onYouTubeIframeAPIReady)
                  How can I stop a video with Javascript in Youtube?(如何在 Youtube 中停止使用 Javascript 的视频?)
                  Embed YouTube video - Refused to display in a frame because it set #39;X-Frame-Options#39; to #39;SAMEORIGIN#39;(嵌入 YouTube 视频 - 拒绝显示在框架中,因为它将“X-Frame-Options设置为“SAMEORIGIN)
                  How to call Greasemonkey#39;s GM_ functions from code that must run in the target page scope?(如何从必须在目标页面范围内运行的代码中调用 Greasemonkey 的 GM_ 函数?)
                  How do you mute an embedded Youtube player?(如何使嵌入式 Youtube 播放器静音?)

                  • <bdo id='Bkxx0'></bdo><ul id='Bkxx0'></ul>
                    <i id='Bkxx0'><tr id='Bkxx0'><dt id='Bkxx0'><q id='Bkxx0'><span id='Bkxx0'><b id='Bkxx0'><form id='Bkxx0'><ins id='Bkxx0'></ins><ul id='Bkxx0'></ul><sub id='Bkxx0'></sub></form><legend id='Bkxx0'></legend><bdo id='Bkxx0'><pre id='Bkxx0'><center id='Bkxx0'></center></pre></bdo></b><th id='Bkxx0'></th></span></q></dt></tr></i><div id='Bkxx0'><tfoot id='Bkxx0'></tfoot><dl id='Bkxx0'><fieldset id='Bkxx0'></fieldset></dl></div>
                  • <small id='Bkxx0'></small><noframes id='Bkxx0'>

                      <tbody id='Bkxx0'></tbody>
                    <legend id='Bkxx0'><style id='Bkxx0'><dir id='Bkxx0'><q id='Bkxx0'></q></dir></style></legend>
                        • <tfoot id='Bkxx0'></tfoot>