js实现全屏和退出全屏

  • Post author:
  • Post category:其他


1.按F11实现的全屏,用api不能退出;用api实现的全屏,可以用api退出全屏;
所以这里监听F11事件并阻止事件, 同时执行自己写的全屏函数
window.addEventListener('keydown',e => {
  e = e || window.event
  if (e.keyCode===122) {
    e.preventDefault()
    this.launchFullscreen()
  }
})
2.全屏按钮则直接执行这个全屏函数

launchFullscreen() {
    let isFullscreen = document.fullScreen || document.mozFullScreen || document.webkitIsFullScreen;
    if(!isFullscreen){
      if (window.ActiveXObject) {
        var WsShell = new ActiveXObject('WScript.Shell')
        WsShell.SendKeys('{F11}');
      }else{
        let el = document.documentElement
        let rfs = el.requestFullScreen || el.webkitRequestFullScreen || el.mozRequestFullScreen || el.msRequestFullscreen
        if (rfs) { // typeof rfs != "undefined" && rfs
          rfs.call(el)
        }
      }
    }else{
      document.exitFullscreen ? document.exitFullscreen() :
      document.mozCancelFullScreen ? document.mozCancelFullScreen() :
      document.webkitExitFullscreen ? document.webkitExitFullscreen() : '';
    }
  },



版权声明:本文为lanbozhiguang原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。