Goal
- I have a video setup that plays fullscreen when a link on the page is tapped on a mobile device. This works fine.
- I have hidden the HTML5 video controls via CSS - I want the video to be completely clean without any controls.
- I want to be able to close the fullscreen video by tapping again the fullscreen video itself.
- I also need to be able to add a class to the body and pause the video on close. The reason for adding the class is that I want to show and hide the video itself, so adding the class lets me control the css for the whole page as a state change.
Problem
When I hit esc
for example, or Back on an Android mobile it obviously won't run the function.
How can I detect when fullscreen has been exited to run whatever functions I want?
Code
videoSmallTrigger.on('click', function() {
// Activate
if (!document.isFullScreen && !document.fullscreenElement && !document.webkitFullscreenElement && !document.mozFullScreenElement && !document.msFullscreenElement) {
console.log('trigger open');
launchFullscreen(videoSmallID);
body.addClass('fullscreen-video-sm');
videoSmall.get(0).play();
}
// Deactivate
else {
console.log('trigger close');
exitFullscreen();
body.removeClass('fullscreen-video-sm');
videoSmall.get(0).pause();
}
});
function launchFullscreen(element) {
if (element.requestFullscreen) {
element.requestFullscreen();
} else if (element.mozRequestFullScreen) {
element.mozRequestFullScreen();
} else if (element.webkitRequestFullscreen) {
element.webkitRequestFullscreen();
} else if (element.msRequestFullscreen) {
element.msRequestFullscreen();
}
}
function exitFullscreen() {
if(document.exitFullscreen) {
document.exitFullscreen();
} else if(document.mozCancelFullScreen) {
document.mozCancelFullScreen();
} else if(document.webkitExitFullscreen) {
document.webkitExitFullscreen();
}
}