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

8 篇文章 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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值