F11全屏功能和js全屏功能冲突解决方法

2024年07月23日 建站教程

功能介绍:右键菜单做了一个显示全屏和取消全屏功能,但是用了F11全屏后,js全屏取消不了,两种冲突了!下面介绍一下解决方法!

var isFull = Math.abs(window.screen.height-window.document.documentElement.clientHeight) <= 17
window.onresize = function () {
    isFull = Math.abs(window.screen.height-window.document.documentElement.clientHeight) <= 17
}
// 阻止F11键默认事件,用HTML5全屏API代替
window.addEventListener('keydown', function (e) {
    e = e || window.event
    if (e.keyCode===122 && !isFull) {
        e.preventDefault()
        enterFullScreen()
    }
})
// 打开浏览器全屏模式
function enterFullScreen () {
  let el = document.documentElement
  let rfs = el.requestFullScreen || el.webkitRequestFullScreen || el.mozRequestFullScreen || el.msRequestFullscreen
  if (rfs) { // typeof rfs != "undefined" && rfs
	rfs.call(el)
  } else if (typeof window.ActiveXObject !== 'undefined') {
	// for IE,这里其实就是模拟了按下键盘的F11,使浏览器全屏
	let wscript = new ActiveXObject('WScript.Shell')
	if (wscript != null) {
	  wscript.SendKeys('{F11}')
	}
  }
}
// 退出全屏
function exitFullScreen () {
  let el = document
  let cfs = el.cancelFullScreen || el.mozCancelFullScreen || el.msExitFullscreen || el.webkitExitFullscreen || el.exitFullscreen
  if (cfs) { // typeof cfs != "undefined" && cfs
	cfs.call(el)
  } else if (typeof window.ActiveXObject !== 'undefined') {
	// for IE,这里和fullScreen相同,模拟按下F11键退出全屏
	let wscript = new ActiveXObject('WScript.Shell')
	if (wscript != null) {
	  wscript.SendKeys('{F11}')
	}
  }
}

本文链接:http://so.lmcjl.com/news/9001/

展开阅读全文
相关内容