使用场景:vue 在全屏模式下需要监听是否按下Esc退出键,做后续操作
但是在使用以下三种方法都没有效果
<div @keyup.up="showBigMap = false" />
mounted() {
//Esc 退出全屏
this.$nextTick(function() {
document.addEventListener('keyup', function(e) {
//此处填写你的业务逻辑即可
if (e.key == 'Escape') {
this.showBigMap = false
}
})
})
},
watch: { //监听退出全屏的变量
'screenfull.isFullscreen': {
handler(value) {
if (!value) {
if (this.showBigMap) {
this.showBigMap = false
}
}
},
immediate:true
}
},
下面上最终实现效果的方法!!!!!
created() {
window.onresize = () => {
if (!this.checkFull()) {
// 退出全屏后要执行的动作
if (this.showBigMap) {
this.showBigMap = false
}
}
}
},
methods: {
// 判断全屏
checkFull() {
//判断浏览器是否处于全屏状态 (需要考虑兼容问题)
//火狐浏览器
var isFull =
document.mozFullScreen ||
document.fullScreen ||
//谷歌浏览器及Webkit内核浏览器
document.webkitIsFullScreen ||
document.webkitRequestFullScreen ||
document.mozRequestFullScreen ||
document.msFullscreenEnabled
if (isFull === undefined) {
isFull = false
}
return isFull
},
}