From 4aa82da11cf608fe8ec8a29ebfc7b89b053b1d80 Mon Sep 17 00:00:00 2001 From: Alec Thomas Date: Sun, 13 Sep 2015 16:51:20 +1000 Subject: [PATCH] Ensure that out-of-viewport videos aren't played. Fixes #15. --- javascripts/app.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/javascripts/app.js b/javascripts/app.js index bb54711..ff7a8c9 100644 --- a/javascripts/app.js +++ b/javascripts/app.js @@ -67,13 +67,15 @@ $(document).ready(function() { } function playVisibleVideos() { - // $('video').each(function(){ - // this.pause(); - // }); - if (!isTouchDevice) { - $('video:in-viewport').each(function(){ - this.play(); + $('video').each(function(){ + if ($(this).is(':in-viewport')) { + if (this.currentTime == 0 || this.paused || this.ended) { + this.play(); + } + } else { + this.pause(); + } }) } } @@ -103,6 +105,8 @@ $(document).ready(function() { throttledVideo = throttle(playVisibleVideos, 100) + $(window).load(throttledVideo); + $(window).scroll(function(){ // Load more JSON from scroll