vue中實(shí)現(xiàn)全屏以及對(duì)退出全屏的監(jiān)聽
前言:
vue中實(shí)現(xiàn)默認(rèn)進(jìn)來(lái)頁(yè)面,某個(gè)div全屏,并監(jiān)聽退出全屏的次數(shù),當(dāng)退出全屏次數(shù)達(dá)到5的時(shí)候跳轉(zhuǎn)到別的頁(yè)面。
實(shí)現(xiàn)步驟:
1、頁(yè)面上在你想要的容器上加上id = ‘con_lf_top_div',再給他加個(gè)動(dòng)態(tài)class名,加上提示和點(diǎn)擊進(jìn)入全屏按鈕

<template>
<el-card
shadow="never"
class="examining"
v-loading.fullscreen.lock="loading"
id="con_lf_top_div"
:class="{'isScreen':!fullscreen}"
>
<p style="color:red;">*溫馨提示:請(qǐng)?jiān)谌料逻M(jìn)行考試,退出全屏5次以后將禁止考試</p>
<el-button v-if="fullscreen" @click="screen();screen()" style="position: absolute;top: 0px;right: 0;">全屏</el-button>
...其他內(nèi)容
2、css部分,全屏后的部分需要單獨(dú)加樣式
.isScreen{
height:100vh!important;
overflow-y: auto;
}
3、js部分
data:
fullscreen:false,//是否全屏 goCount:0 //退出第幾次
mounted初始化調(diào)用
mounted() {
this.initScreen()
}
methods定義方法:

//初始化全屏方法
initScreen(){
this.goCount = 0
this.screen() //打開全屏
window.addEventListener('keydown', function(event) {
//禁掉F11的全屏的默認(rèn)事件,不會(huì)禁止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('當(dāng)前是退出第'+this.goCount+'次')
console.log('當(dāng)前是退出第'+this.goCount+'次')
this.fullscreen = true
if(this.goCount == 5){
this.goBack()
}
}
})
},


完整源碼:
1、頁(yè)面:
<el-card
id="con_lf_top_div"
:class="{'isScreen':!fullscreen}"
>
<p style="color:red;">*溫馨提示:請(qǐng)?jiān)谌料逻M(jìn)行考試,退出全屏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的全屏的默認(rèn)事件,不會(huì)禁止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++
// 注意這里的事件都會(huì)觸發(fā)兩次
console.log('當(dāng)前是退出第'+this.goCount+'次')
this.fullscreen = true
if(this.goCount == 5){
this.goBack()
}
}
})
},
//全屏方法
screen(){
//設(shè)置后就是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次,當(dāng)前考試已經(jīng)結(jié)束')
this.$router.go(-1)
},
更多資料:
https://blog.csdn.net/qq_41619796/article/details/104751814
https://blog.csdn.net/wangsiyisiyi/article/details/117086453
到此這篇關(guān)于vue中實(shí)現(xiàn)全屏以及對(duì)退出全屏的監(jiān)聽的文章就介紹到這了,更多相關(guān)vue中實(shí)現(xiàn)全屏以及對(duì)退出全屏的監(jiān)聽內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
手把手教你創(chuàng)建vue3項(xiàng)目的最佳方式
如今的Vue3已經(jīng)勢(shì)不可擋,當(dāng)然搭建一個(gè)全新的Vue3項(xiàng)目也有了全新的方式,下面這篇文章主要給大家介紹了關(guān)于如何手把手教你創(chuàng)建vue3項(xiàng)目的最佳方式,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11
Vue如何獲取new Date().getTime()時(shí)間戳
在Web開發(fā)中,前端使用Vue.js獲取的是毫秒級(jí)時(shí)間戳,而PHP后端則是秒級(jí)時(shí)間戳,處理此類問題時(shí),需要將PHP的時(shí)間戳乘以1000轉(zhuǎn)換為毫秒級(jí),以保證數(shù)據(jù)的一致性和正確的邏輯判斷2024-10-10
vue3按鈕點(diǎn)擊頻率控制的實(shí)現(xiàn)示例
在前端開發(fā)中,當(dāng)用戶頻繁連續(xù)點(diǎn)擊按鈕,可能會(huì)導(dǎo)致頻繁的請(qǐng)求或者觸發(fā)過(guò)多的操作,本文主要介紹了vue3按鈕點(diǎn)擊頻率控制的實(shí)現(xiàn)示例,感興趣的可以了解一下2024-01-01
深入了解vue中一鍵復(fù)制功能的實(shí)現(xiàn)
在現(xiàn)代的Web應(yīng)用中,用戶體驗(yàn)至關(guān)重要,而提供簡(jiǎn)單易用的復(fù)制功能是改善用戶體驗(yàn)的一項(xiàng)關(guān)鍵功能,本文將為大家詳細(xì)介紹Vue實(shí)現(xiàn)一鍵復(fù)制功能的具體方法,需要的可以參考下2023-11-11
Vue2.x Todo之自定義指令實(shí)現(xiàn)自動(dòng)聚焦的方法
我們希望用戶雙擊 todo 進(jìn)入編輯狀態(tài)后輸入框自動(dòng)獲取焦點(diǎn),而不是需要先手動(dòng)點(diǎn)一下。這篇文章主要介紹了Vue 2.x Todo之自定義指令實(shí)現(xiàn)自動(dòng)聚焦,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2019-01-01
詳解Vue中Computed與watch的用法與區(qū)別
這篇文章主要介紹了Vue中computed和watch的使用與區(qū)別,文中通過(guò)示例為大家進(jìn)行了詳細(xì)講解,對(duì)Vue感興趣的同學(xué),可以學(xué)習(xí)一下2022-04-04

