How to play pause video on scroll

后端 未结 2 1069
被撕碎了的回忆
被撕碎了的回忆 2021-01-25 14:43

I want to play or pause video on scroll, if scroll is greater than 300 it should pause otherwise it should play. This is my video tag

2条回答
  •  我在风中等你
    2021-01-25 15:39

    You need to bind your function to the scroll event and also change from autoplay to actually play() - pause(), check this example snippet:

    Note: I have changed from 300 to 70 just for the example but you can keep your breakpoint as you want

    var myvid = $('#myVid')[0];
    $(window).scroll(function(){
      var scroll = $(this).scrollTop();
      scroll > 70 ? myvid.pause() : myvid.play()
    })
    body {
      background:#e1e1e1;
      height:1000px;
    }
    video {
      display:block;
      width:300px;
      margin:0 auto;
    }
    
    

提交回复
热议问题