vue 全屏/退出全屏,esc监控退出全屏

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 16:47   3245   0

template:

<el-button class="fullbutton" type="primary" plain @click="fullScreen">{{fullScreenFlag?'退出全屏':'全屏'}}</el-button>

js:

data () {
    return {
      fullScreenFlag: false, // 全屏/退出全屏
    }
  },
mounted () {
    document.addEventListener("fullscreenchange", this.fullScreenEsc)
  },
  destroyed () {
    document.removeEventListener("fullscreenchange", this.fullScreenEsc)
  },
fullScreen () {
      // this.$refs.iframe.requestFullscreen()
      this.fullScreenFlag = !this.fullScreenFlag
      if (this.fullScreenFlag) {
        this.$refs.wrapper.requestFullscreen()
      } else {
        document.exitFullscreen()
      }
    },
    fullScreenEsc () {
      if (!this.checkFull()) {
        this.fullScreenFlag = false
      }

    },
    checkFull () {
      var isFull =
        document.fullscreenElement ||
        document.mozFullScreenElement ||
        document.webkitFullscreenElement;
      //to fix : false || undefined == undefined
      if (isFull === undefined) isFull = false;
      return isFull;
    },

注意:监控esc退出全屏的方式,如果采用监听keydown的事件(key.code===27),全屏时会监控不到按键,因此采用监听fullscreenchange事件,同时用如上checkFull函数判断是否全屏

参考:

https://blog.csdn.net/weixin_44770377/article/details/106090877

keydown无法获取esc,参考处理:

https://blog.csdn.net/xiaoyanli8077/article/details/89537911

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP