HTML5 and Javascript to play videos only when visible

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();
    }
})

Leave a Comment