vue中实现全屏以及对退出全屏的监听
程序员文章站
2022-05-03 12:05:32
前言:vue中实现默认进来页面,某个div全屏,并监听退出全屏的次数,当退出全屏次数达到5的时候跳转到别的页面。实现步骤:1、页面上在你想要的容器上加上id = ‘con_lf_top_div',再给...
前言:
vue中实现默认进来页面,某个div全屏,并监听退出全屏的次数,当退出全屏次数达到5的时候跳转到别的页面。
实现步骤:
1、页面上在你想要的容器上加上id = ‘con_lf_top_div',再给他加个动态class名,加上提示和点击进入全屏按钮
<template> <el-card shadow="never" class="examining" v-loading.fullscreen.lock="loading" id="con_lf_top_div" :class="{'isscreen':!fullscreen}" > <p style="color:red;">*温馨提示:请在全屏下进行考试,退出全屏5次以后将禁止考试</p> <el-button v-if="fullscreen" @click="screen();screen()" style="position: absolute;top: 0px;right: 0;">全屏</el-button> ...其他内容
2、css部分,全屏后的部分需要单独加样式
.isscreen{ height:100vh!important; overflow-y: auto; }
3、js部分
data:
fullscreen:false,//是否全屏 gocount:0 //退出第几次
mounted初始化调用
mounted() { this.initscreen() }
methods定义方法:
//初始化全屏方法 initscreen(){ this.gocount = 0 this.screen() //打开全屏 window.addeventlistener('keydown', function(event) { //禁掉f11的全屏的默认事件,不会禁止f11的退出全屏 const e = event || window.event if (e && e.keycode === 122) { e.preventdefault() } }) document.addeventlistener('fullscreenchange', v => { if(this.fullscreen == true){ this.fullscreen = false }else{ this.gocount++ // this.$message.info('当前是退出第'+this.gocount+'次') console.log('当前是退出第'+this.gocount+'次') this.fullscreen = true if(this.gocount == 5){ this.goback() } } }) },
完整源码:
1、页面: <el-card id="con_lf_top_div" :class="{'isscreen':!fullscreen}" > <p style="color:red;">*温馨提示:请在全屏下进行考试,退出全屏5次以后将禁止考试</p> <el-button v-if="fullscreen" @click="screen();screen()" style="position: absolute;top: 0px;right: 0;">全屏</el-button> ... 2、data: fullscreen:false,//是否全屏 gocount:0 //退出第几次 3、mounted: this.initscreen() 4、methods: //初始化全屏方法 initscreen(){ this.gocount = 0 this.screen() //打开全屏 window.addeventlistener('keydown', function(event) { //禁掉f11的全屏的默认事件,不会禁止f11的退出全屏 const e = event || window.event if (e && e.keycode === 122) { e.preventdefault() } }) document.addeventlistener('fullscreenchange', v => { if(this.fullscreen == true){ this.fullscreen = false }else{ this.gocount++ // 注意这里的事件都会触发两次 console.log('当前是退出第'+this.gocount+'次') this.fullscreen = true if(this.gocount == 5){ this.goback() } } }) }, //全屏方法 screen(){ //设置后就是id==con_lf_top_div 的容器全屏 let element = document.getelementbyid('con_lf_top_div'); 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; }, //退出全屏方法 goback(){ //111111111111111111111111111111111111111 this.$message.error('您已退出全屏5次,当前考试已经结束') this.$router.go(-1) },
更多资料:
到此这篇关于vue中实现全屏以及对退出全屏的监听的文章就介绍到这了,更多相关vue中实现全屏以及对退出全屏的监听内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!