I'm trying to get my embedded Vimeo video to pause on exiting the viewport and allow another embedded Vimeo video to play upon entering viewport.
I've been exploring various solutions and discovered Froogaloop which works perfectly, however I believe is the old Vimeo api.
I got an exact example working of the video pausing on exiting, but its using an ID rather than class so unable to use on more than one video and not entirely sure how to accomplish this.
Example - Jsfiddle
<div class="inner">
<iframe id="video" src="https://player.vimeo.com/video/220643186?
autoplay=1&loop=1&color=357ded&title=0&byline=0&portrait=0" width="640"
height="360" frameborder="0" webkitallowfullscreen mozallowfullscreen
allowfullscreen></iframe>
</div>
Jquery :
var iframe = document.getElementById('video');
// $f == Froogaloop
var player = $f(iframe);
var inner = $(".inner");
var elementPosTop = inner.position().top;
var viewportHeight = $(window).height();
$(window).on('scroll', function () {
var scrollPos = $(window).scrollTop();
var elementFromTop = elementPosTop - scrollPos;
if (elementFromTop > 0 && elementFromTop < elementPosTop + viewportHeight) {
inner.addClass("active");
player.api("play");
} else {
inner.removeClass("active");
player.api("pause");
}
});
Any help would be immensely appreciated :)