ie11 在元素全屏的情况下可以监听到 keydown 事件(谷歌监听不到),使用 keydown 事件来区别是否在F11全屏状态下切换的元素全屏。
高版本浏览器监听 fullscreenchange 来控制全屏的切换,不会受F11全屏的影响。(resize 在 F11的全屏时不触发)
methods: {
// 全屏
fullScreen() {
var el = this.$el
var rfs = el.requestFullscreen || el.webkitRequestFullscreen || el.mozRequestFullscreen || el.msRequestFullscreen
if (rfs) {
rfs.call(el)
if(rfs === el.msRequestFullscreen){
this.onResize()
}
} else if (typeof window.ActiveXObject !== 'undefined') {
//for IE,这里其实就是模拟了按下键盘的F11,使浏览器全屏
var wscript = new ActiveXObject('WScript.Shell')
if (wscript != null) {
wscript.SendKeys('{F11}')
}
}
},
// 退出全屏
exitScreen() {
var el = document
var cfs =
el.exitFullscreen ||
el.cancelFullscreen ||
el.webkitCancelFullscreen ||
el.mozCancelFullScreen ||
el.msExitFullscreen
if (cfs) {
cfs.call(el)
if(cfs === el.msExitFullscreen){
this.onResize()
}
} else if (typeof window.ActiveXObject !== 'undefined') {
//for IE,这里和fullScreen相同,模拟按下F11键退出全屏
var wscript = new ActiveXObject('WScript.Shell')
if (wscript != null) {
wscript.SendKeys('{F11}')
}
}
},
//全屏切换时
onResize() {
console.log('------resize')
let isFullscreen = document.fullscreenElement || document.msFullscreenElement
this.fullScreenFlag = isFullscreen === this.$el ? true : false
this.$refs.content.style.height = this.fullScreenFlag ? 'calc(100% - 100px)' : '100%'
},
onKeydowm(e){
// 全屏取消之后才触发的 keydown
if(e.keyCode === 27 && this.$el.msRequestFullscreen){
this.fullScreenFlag = false
this.$refs.content.style.height = '100%'
}
}
},
mounted() {
this.$nextTick(() => {
this.fullScreen()
})
// 兼容IE11
window.addEventListener('keydown',this.onKeydowm)
// 高版本浏览器
document.addEventListener("fullscreenchange", this.onResize)
},