一、打开全屏

requestFullscreen () {
  const docElm = document.documentElement
  if (docElm.requestFullscreen) {
    docElm.requestFullscreen()
  } else if (docElm.msRequestFullscreen) {
    docElm.msRequestFullscreen()
  } else if (docElm.mozRequestFullScreen) {
    docElm.mozRequestFullScreen()
  } else if (docElm.webkitRequestFullScreen) {
    docElm.webkitRequestFullScreen()
  }
}

二、判断是否处于全屏状态

function isFullscreen () {
  return document.fullscreenElement ||
  document.msFullscreenElement ||
  document.mozFullScreenElement ||
  document.webkitFullscreenElement || false;
}

三、退出全屏

  exitFullScreen () {
    if(isFullscreen()){
      if (document.exitFullscreen) {
        document.exitFullscreen()
      } else if (document.msExitFullscreen) {
        document.msExitFullscreen()
      } else if (document.mozCancelFullScreen) {
        document.mozCancelFullScreen()
      } else if (document.webkitCancelFullScreen) {
        document.webkitCancelFullScreen()
      }
    }
}

四、注意点

退出全屏的时候,一定要先执行isFullscreen函数,看一下当前是处于全屏状态,才能再执行关闭全屏的代码,否则不是处于全屏状态执行退出全屏代码就会报错

 

posted on 2022-10-18 14:58  铭の  阅读(582)  评论(0编辑  收藏  举报

友情链接:箫竹影(Java工程师)