vue fullscreen and cancel fullscreen

Code:

<div class="alignCenter mr10 active_t" @click="handleFullScreen" style="font-size: 16px;color: #999;">
                        <Icon type="md-resize" style="font-size: 16px;" />
                        <span style="margin-left:2px;">{
   
   {fullscreen?`取消全屏`:`全屏`}}</span>
                    </div>
// 全屏事件
        handleFullScreen() {
                // 此处可根据获取节点进行区域全屏事件
            let element = document.documentElement;
            if (this.fullscreen) {
                if (document.exitFullscreen) {
                    document.exitFullscreen();
                } else if (document.webkitCancelFullScreen) {
                    document.webkitCancelFullScreen();
                } else if (document.mozCancelFullScreen) {
                    document.mozCancelFullScreen();
                } else if (document.msExitFullscreen) {
                    document.msExitFullscreen();
                }
            } else {
                if (element.requestFullscreen) {
                    element.requestFullscreen();
                } else if (element.webkitRequestFullScreen) {
                    element.webkitRequestFullScreen();
                } else if (element.mozRequestFullScreen) {
                    element.mozRequestFullScreen();
                } else if (element.msRequestFullscreen) {
                    // IE11
                    element.msRequestFullscreen();
                }
            }
            this.fullscreen = !this.fullscreen;
        },

Considering that if you click to upload a file or control the full screen with shortcut keys, the status will be inconsistent, so you need to monitor the full screen status in mounted:

        var that=this;
        //监听window是否全屏,并进行相应的操作,支持esc键退出
        window.onresize = function() {
            var isFull=!!(document.webkitIsFullScreen || document.mozFullScreen || 
                document.msFullscreenElement || document.fullscreenElement
            );//!document.webkitIsFullScreen都为true。因此用!!
            if (isFull==false) {
                that.fullscreen = false;
            }else{
                that.fullscreen = true;
            }
        }

Guess you like

Origin blog.csdn.net/qq_42740797/article/details/123359200