javascript - 看不见时暂停/恢复 Youtube 视频(滚动)

标签 javascript jquery html video youtube

我正在使用一个能够播放背景全 Angular youtube 视频的 javascript 插件:

<script>
            $(document).ready(function () {

        $(".player").mb_YTPlayer();

    });
</script>

<script>
            var vids = ["list of videos"
            ]; // create your list of youtube videos

    var currVid = vids[Math.floor(Math.random()*(vids.length-1))]; // this will grab a random video from the array.

    var opts = { // keep all the options in an object
      videoURL: currVid, // set the video to display
      containment:'.video-section',
      quality:'large', 
      autoPlay:true, 
      mute:true, 
      opacity:1
    }

    $(document).ready(function(){
      document.getElementById("bgndVideo").dataset.property = JSON.stringify(opts); // change to this

      $(".player").mb_YTPlayer(); // play!
    });
</script>

当我在网站上向下滚动并在视口(viewport)中恢复时,如何让它暂停?

谢谢。

最佳答案

我建议添加 scroll容器的事件监听器(在您的情况下可能是 window 对象,然后在每次滚动时尝试计算视频是否在可见视口(viewport)中。这里有一些有用的代码:

$(window).on("scroll", function() {
    var viewportTop = window.scrollY;
    var viewportBottom = viewportTop + window.outerHeight;
    var isVideoBelowViewport = $(".player").offset().top > viewportBottom;
    var isVideoAboveViewport = $(".player").offset().bottom < viewportTop;
    var isVideoOutsideViewport = isVideoBelowViewport || isVideoAboveViewport;
    if ( isVideoOutsideViewport && isVideoPlaying() ) pauseVideo();
    if ( !isVideoOutsideViewport && !isVideoPlaying() ) resumeVideo();
});

我会留给你写isVideoPlaying() , resumeVideo()pauseVideo() .

关于javascript - 看不见时暂停/恢复 Youtube 视频(滚动),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46354668/

相关文章:

javascript - 使用 DOM 将 SVG 元素添加到现有 SVG

javascript - RegEx如何修改为只接受1-9之间的1位数字JavaScript

javascript - 如何使用 JQuery 在 2 个元素之间画一条线并刷新该线?

html - HTML 中的退格键

javascript - 是否可以在 HTML5 网页上接受手写笔输入

javascript - 返回时恢复页面内容

javascript - 我应该使用什么 Meteor 事件处理程序来提交 onchange 表单

javascript - 光廊图片下载问题

javascript - 无法访问另一个javascript文件中的变量

html - 到底是什么导致宽度比我网页上的宽度大?