Skip to content

Instantly share code, notes, and snippets.

@gunlinux
Last active October 16, 2015 06:46
Show Gist options
  • Save gunlinux/b3fc50658438ddf9bd92 to your computer and use it in GitHub Desktop.
Save gunlinux/b3fc50658438ddf9bd92 to your computer and use it in GitHub Desktop.
var play_visible_video = function () {
$('.background-video').each(function () {
var video = $(this);
var view = $(window);
var viewTop = view.scrollTop();
var viewBottom = viewTop + view.height();
var videoTop = video.offset().top;
var videoHeight = video.height();
var videoMiddle = videoTop + 0.5 * videoHeight;
if ((viewBottom > videoMiddle) && (viewTop < videoMiddle)) {
video[0].play();
video.loop = true;
} else {
video[0].pause();
}
});
};
$(window).on('scroll', function () {
play_visible_video();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment