HTML5 and Javascript to play videos only when visible

前端 未结 11 656
梦如初夏
梦如初夏 2020-12-02 12:25

I am interested in setting up an HTML page with multiple video clips such that each video clip plays only while visible and then pauses when out of view.

I have fo

11条回答
  •  猫巷女王i
    2020-12-02 13:23

    Need to check if the video is visible during the scrolling.

     $(window).scroll(function() {
        $('video').each(function(){
            if ($(this).is(":in-viewport")) {
                $(this)[0].play();
            } else {
                $(this)[0].pause();
            }
        })
    });
    

提交回复
热议问题