<template>
<div>
<div id="screen" @click="screen">Toggle Fullscreen</div>
</div>
</template>
<script>
export default {
data() {
return {
fullscreen: false
};
},
mounted() {
document.addEventListener('fullscreenchange', this.handleFullScreenChange);
document.addEventListener('keydown', this.handleKeyDown);
},
beforeDestroy() {
document.removeEventListener('fullscreenchange', this.handleFullScreenChange);
document.removeEventListener('keydown', this.handleKeyDown);
},
methods: {
handleFullScreenChange() {
if (!document.fullscreenElement) {
this.fullscreen = false;
}
},
handleKeyDown(event) {
if (event.key === 'Escape') {
if (this.fullscreen) {
this.exitFullScreen();
}
}
},
screen() {
let element = document.getElementById('screen');
if (this.fullscreen) {
this.exitFullScreen();
} else {
this.enterFullScreen(element);
}
this.fullscreen = !this.fullscreen;
},
enterFullScreen(element) {
if (element.requestFullscreen) {
element.requestFullscreen();
} else if (element.webkitRequestFullScreen) {
element.webkitRequestFullScreen();
} else if (element.mozRequestFullScreen) {
element.mozRequestFullScreen();
} else if (element.msRequestFullscreen) {
element.msRequestFullscreen();
}
},
exitFullScreen() {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.webkitCancelFullScreen) {
document.webkitCancelFullScreen();
} else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
} else if (document.msExitFullscreen) {
document.msExitFullscreen();
}
}
}
};
</script>