当用户滚动离开时,我如何暂停youtube嵌入



我一直在努力寻找帮助,当用户滚动离开时,视频暂停。我已经能够找到html5视频的帮助,但现在我还需要知道如何youtube API可以用于相同的。

嵌入YouTube的html结构如下

     <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有一个非常不同的方法来做同样的?

    <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我能够找到一个代码,阻止一个玩家,如果另一个正在玩,但我不知道如何可以操纵,以实现我所需要的。

<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。

试试我做的这个实例:http://jsbin.com/cocatuta/15/edit?js,output

所以基本上我只是替换了:

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

By this:

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();

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

相关内容

  • 没有找到相关文章

最新更新