var button = document.querySelector('#container .button'); button.addEventListener('click', fullscreen);
function fullscreen() { // check if fullscreen mode is available if (document.fullscreenEnabled || document.webkitFullscreenEnabled || document.mozFullScreenEnabled || document.msFullscreenEnabled) {
// which element will be fullscreen var iframe = document.querySelector('#container iframe'); // Do fullscreen if (iframe.requestFullscreen) { iframe.requestFullscreen(); } elseif (iframe.webkitRequestFullscreen) { iframe.webkitRequestFullscreen(); } elseif (iframe.mozRequestFullScreen) { iframe.mozRequestFullScreen(); } elseif (iframe.msRequestFullscreen) { iframe.msRequestFullscreen(); } } else { document.querySelector('.error').innerHTML = 'Your browser is not supported'; } }
// when you are in fullscreen, ESC and F11 may not be trigger by keydown listener. // so don't use it to detect exit fullscreen document.addEventListener('keydown', function (e) { console.log('key press' + e.keyCode); });
// detect enter or exit fullscreen mode document.addEventListener('webkitfullscreenchange', fullscreenChange); document.addEventListener('mozfullscreenchange', fullscreenChange); document.addEventListener('fullscreenchange', fullscreenChange); document.addEventListener('MSFullscreenChange', fullscreenChange);
functionfullscreenChange() { if (document.fullscreenEnabled || document.webkitIsFullScreen || document.mozFullScreen || document.msFullscreenElement) { console.log('enter fullscreen'); } else { console.log('exit fullscreen'); } // force to reload iframe once to prevent the iframe source didn't care about trying to resize the window // comment this line and you will see var iframe = document.querySelector('iframe'); iframe.src = iframe.src; }