Pause and play video when in viewport

后端 未结 3 1820
别那么骄傲
别那么骄傲 2021-01-17 05:43

I was experimenting with play and pause when a video is within the viewport... when I was searching around I found the following code.. which unfortunately didn\'t work:

3条回答
  •  清歌不尽
    2021-01-17 06:00

    $(window).scroll(function(e)
      {
        var offsetRange = $(window).height() / 3,
            offsetTop = $(window).scrollTop() + offsetRange + $("#header").outerHeight(true),
            offsetBottom = offsetTop + offsetRange;
    
        $(".video").each(function () { 
          var y1 = $(this).offset().top;
          var y2 = offsetTop;
          if (y1 + $(this).outerHeight(true) < y2 || y1 > offsetBottom) {
            this.pause(); 
          } else {
            this.play();
          }
        });
    });
    

提交回复
热议问题