Skip to content

Instantly share code, notes, and snippets.

@dgrebb
Created June 15, 2017 15:19
Show Gist options
  • Save dgrebb/10fc9f3140a26b355b9056468c8f5c5f to your computer and use it in GitHub Desktop.
Save dgrebb/10fc9f3140a26b355b9056468c8f5c5f to your computer and use it in GitHub Desktop.
viewport scroll in/out event
/**
* Play or pause video based on visibility in window viewport.
*
* @param {any} video The video element.
*
* @memberof TabbedVideo
*/
toggleVideoPlayback(video) {
var windowHeight = window.innerHeight,
videoTop = video.getBoundingClientRect().top,
videoBottom = videoTop + video.offsetHeight;
if ( videoTop < windowHeight && videoBottom > 0 ) {
video.play();
}
if ( videoBottom <= 0 || videoTop > windowHeight ) {
video.pause();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment