HTML5 and Javascript to play videos only when visible

前端 未结 11 630
梦如初夏
梦如初夏 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条回答
  •  攒了一身酷
    2020-12-02 13:02

    Using the isInViewport plugin and jQuery, here's my code for the task

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

提交回复
热议问题