I am trying to Pause/play different videos once each reach the centre of the viewport, then pause them once you scroll away from them.
I am using JWPlayer but I am having issues pausing all the other videos on the page and keeping the current one playing that visible in the viewport.
https://jsfiddle.net/vv7jbrv6/1/
function onScrollVideo() {
updateVideo();
}
function updateVideo() {
var windowHeight = $(window).height(),
gridTop = 200,
gridBottom = 1000;
$('.jwplayer').each(function() {
var thisTop = $(this).offset().top - $(window).scrollTop(),
playerID = $(this).prop('id');
if (thisTop > gridTop && (thisTop + $(this).height()) < gridBottom) {
jwplayer(playerID).play();
} else {
jwplayer(playerID).on('pause');
}
});
}
window.addEventListener('scroll', onScrollVideo, false);