当前位置: 首页 > article >正文

vue全屏,退出全屏、监听ESC退出全屏

一、所有内容全屏

<template>
    <div class="Quanping">
        <el-button style="position: absolute;top: 0%;right: 0%;background-color: palegreen" @click="toFullOrExit()">
                {{ isFull ? '关闭全屏' : '全屏' }}
            </el-button>
     </div>
</template>
 
<script>
export default {
  data()
  {
    return {
        isFull: false, //全屏显示true
      }
  },
  created()
  {
    
  },
  mounted()
  {
 
  },
  methods: {
    //全屏----start
            toFullOrExit() {
                this.isFull = !this.isFull
                if (this.isFull) {
                    this.requestFullScreen()
                } else {
                    this.exitFullscreen()
                }
            },
            requestFullScreen() {
                let de = document.documentElement
                if (de.requestFullscreen) {
                    de.requestFullscreen()
                } else if (de.mozRequestFullScreen) {
                    de.mozRequestFullScreen()
                } else if (de.webkitRequestFullScreen) {
                    de.webkitRequestFullScreen()
                }
            },
            exitFullscreen() {
                let de = document
                if (de.exitFullscreen) {
                    de.exitFullscreen()
                } else if (de.mozCancelFullScreen) {
                    de.mozCancelFullScreen()
                } else if (de.webkitCancelFullScreen) {
                    de.webkitCancelFullScreen()
                }
            },
            //全屏----end
  }
}
</script>

效果跟F11效果一样

二、部分内容全屏

<template>
    <div class="Quanping"  id="screen">
        <el-button style="position: absolute;top: 0%;right: 0%;background-color: palegreen" @click="screen()">
                 {{ fullscreen ? '关闭全屏' : '全屏' }}
            </el-button>
     </div>
</template>
 
<script>
export default {
  data()
  {
    return {
        fullscreen: false, //全屏显示true
      }
  },
  created()
  {
  },
  mounted()
  {
 // 添加全屏事件监听器
            this.fullListen()
  },
  methods: {

            // 绑定监听事件
            fullListen() {
                let screen = document.getElementById("screen");
                if (screen.requestFullscreen) {
                    document.addEventListener("fullscreenchange", () => {
                        if (this.getFullscreenElement() == null) {
                            this.fullscreen = false;
                            this.buttonName = this.fullscreen ? "关闭全屏" : "全屏展示"
                        }
                    })
                }
            },
            // 获取全屏状态
            getFullscreenElement() {
                return (
                    document['fullscreenElement'] ||
                    document['mozFullScreenElement'] ||
                    document['msFullScreenElement'] ||
                    document['webkitFullscreenElement'] || null
                );
            },
    //全屏----start
            screen() {
                let element = document.getElementById('screen');
                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;
            },
            //全屏----end
  }
}
</script>

三、监听ESC退出全屏事件

// 绑定监听事件
            fullListen() {
                let screen = document.getElementById("screen");
                if (screen.requestFullscreen) {
                    document.addEventListener("fullscreenchange", () => {
                        if (this.getFullscreenElement() == null) {
                            this.fullscreen = false;
                            this.buttonName = this.fullscreen ? "关闭全屏" : "全屏展示"
                        }
                    })
                }
            },
            // 获取全屏状态
            getFullscreenElement() {
                return (
                    document['fullscreenElement'] ||
                    document['mozFullScreenElement'] ||
                    document['msFullScreenElement'] ||
                    document['webkitFullscreenElement'] || null
                );
            },
mounted() {
            // 添加全屏事件监听器
            this.fullListen()
        },


http://www.kler.cn/a/228830.html

相关文章:

  • 安路FPGA开发工具TD:问题解决办法 及 Tips 总结
  • 周末总结(2024/01/18)
  • 20250118-读取并显示彩色图像以及提取彩色图像的 R、G、B 分量
  • postcss插件-实现vw适配
  • HarmonyOS NEXT应用开发边学边玩系列:从零实现一影视APP (三、影视搜索页功能实现)
  • 重拾Python学习,先从把python删除开始。。。
  • 01背包问题 动态规划
  • CAN通信----(创芯科技)CAN分析仪----转CANTest使用
  • 2024年2月CCF-全国精英算法大赛题目
  • 前端面试题——Vue的双向绑定
  • <网络安全>《16 网络安全隔离与信息单向导入系统》
  • 计算机视觉实战项目3(图像分类+目标检测+目标跟踪+姿态识别+车道线识别+车牌识别+无人机检测+A*路径规划+单目测距与测速+行人车辆计数等)
  • 【HarmonyOS应用开发】Web组件的使用(十三)
  • 壹[1],Xamarin开发环境配置
  • linux的nginx安装
  • 复旦大学NLP团队发布86页大模型Agent综述
  • Git私服搭建
  • UML---用例图,类图
  • 前端如何预防CSRF
  • python的进程,线程、协程
  • 群晖NAS开启FTP服务结合内网穿透实现公网远程访问本地服务
  • Unity3D开发之鼠标单双击判断
  • 如何在PS5上使用金手指修改游戏
  • docker 离线安装镜像
  • Day05-Linux bash核心介绍及目录命令讲解
  • Day 17------C语言收尾之链表的删除、位运算、预处理、宏定义