Skip to content

Instantly share code, notes, and snippets.

@alloyking
Last active May 11, 2016 02:43
Show Gist options
  • Save alloyking/03dc250737b3fc643957f2b544760d23 to your computer and use it in GitHub Desktop.
Save alloyking/03dc250737b3fc643957f2b544760d23 to your computer and use it in GitHub Desktop.
off screen pause
play_in_view: function() {
percent_of_video_visible = 0.2;
function check_videos_in_view() {
$('.ambient video').each(function() {
var x = $(this).offset().left,
y = $(this).offset().top,
w = $(this).outerWidth(),
h = $(this).outerHeight(),
r = x + w, //right
b = y + h, //bottom
visibleX,
visibleY,
visible;
visibleX = Math.max(0, Math.min(w, window.pageXOffset + window.innerWidth - x, r - window.pageXOffset));
visibleY = Math.max(0, Math.min(h, window.pageYOffset + window.innerHeight - y, b - window.pageYOffset));
visible = visibleX * visibleY / (w * h);
if (visible > percent_of_video_visible) {
this.play();
} else {
this.pause();
}
});
}
window.addEventListener('scroll', check_videos_in_view, false);
window.addEventListener('resize', check_videos_in_view, false);
check_videos_in_view();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment