Skip to content

Instantly share code, notes, and snippets.

@ragoand
Created March 13, 2023 10:18
Show Gist options
  • Save ragoand/35fe0b1e719ec6004f5397ff36b2bb17 to your computer and use it in GitHub Desktop.
Save ragoand/35fe0b1e719ec6004f5397ff36b2bb17 to your computer and use it in GitHub Desktop.
Play/Pause a Vimeo Video on window focus change
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
<script src="https://player.vimeo.com/api/player.js"></script>
<script>
jQuery(document).ready(function() {
let hasFocus = true;
var videoPlayer = new Vimeo.Player(jQuery('.stm_lms_video__iframe'));
setInterval(checkFocus, 200);
function checkFocus() {
let currentFocus = document.hasFocus();
var currentDate = '[' + new Date().toUTCString() + '] ';
if ( currentFocus != hasFocus && currentFocus ) {
videoPlayer.play();
console.log("The document has the focus.", currentDate);
hasFocus = true;
}
if( currentFocus != hasFocus && !currentFocus ) {
console.log("The document doesn't have the focus.", currentDate);
hasFocus = false;
videoPlayer.pause();
}
}
})
</script>
</head>
<body>
<div class="stm_lms_video__iframe">
<iframe src="https://player.vimeo.com/video/286183716" width="640" height="360" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment