template:
<el-button class="fullbutton" type="primary" plain @click="fullScreen">{{fullScreenFlag?'退出全屏':'全屏'}}</el-button>
js:
data () {
return {
fullScreenFlag: false, // 全屏/退出全屏
}
},
mounted () {
document.addEventListener("fullscreenchange", this.fullScreenEsc)
},
destroyed () {
document.removeEventListener("fullscreenchange", this.fullScreenEsc)
},
fullScreen () {
// this.$refs.iframe.requestFullscreen()
this.fullScreenFlag = !this.fullScreenFlag
if (this.fullScreenFlag) {
this.$refs.wrapper.requestFullscreen()
} else {
document.exitFullscreen()
}
},
fullScreenEsc () {
if (!this.checkFull()) {
this.fullScreenFlag = false
}
},
checkFull () {
var isFull =
document.fullscreenElement ||
document.mozFullScreenElement ||
document.webkitFullscreenElement;
//to fix : false || undefined == undefined
if (isFull === undefined) isFull = false;
return isFull;
},
注意:监控esc退出全屏的方式,如果采用监听keydown的事件(key.code===27),全屏时会监控不到按键,因此采用监听fullscreenchange事件,同时用如上checkFull函数判断是否全屏
参考:
https://blog.csdn.net/weixin_44770377/article/details/106090877
keydown无法获取esc,参考处理:
https://blog.csdn.net/xiaoyanli8077/article/details/89537911