基于Vue實(shí)現(xiàn)頁(yè)面全屏封裝的詳細(xì)步驟
前言
眾所周知:目前可視化大屏,視頻播放等常見功能都需要用到全屏。本文將使用兩種技術(shù)實(shí)現(xiàn)全屏功能的封裝,讓不同技術(shù)棧的同學(xué)都可以輕松掌握。好了,讓我們來實(shí)現(xiàn)一個(gè)既兼容性強(qiáng)又易于管理的全屏功能組件吧。
技術(shù)方案
1. vue3 + screenfull庫(kù)
2. 原生js
一、vue3 + screenfull庫(kù) 方案
第一步:準(zhǔn)備工作
首先,確保你已經(jīng)安裝了Vue CLI。如果沒有,那就趕緊的:
npm install -g @vue/cli
然后,創(chuàng)建一個(gè)新的Vue 3項(xiàng)目:
vue create fullscreen-vue cd fullscreen-vue
接下來,安裝screenfull,這個(gè)小小的庫(kù)將是我們實(shí)現(xiàn)全屏功能的大功臣:
npm install screenfull
第二步:創(chuàng)建全屏組件
在src/components目錄下,創(chuàng)建一個(gè)名為FullscreenComponent.vue的文件。這里,我們將編寫我們的全屏組件。
<template>
<div>
<!-- 按鈕用于觸發(fā)全屏切換 -->
<button @click="toggleFullscreen">一鍵全屏</button>
<!-- 全屏內(nèi)容區(qū)域,使用ref進(jìn)行引用 -->
<div ref="fullscreenElement" class="fullscreen-content">
<p>看我看我,我是全屏內(nèi)容!</p>
</div>
</div>
</template>
<script>
import screenfull from 'screenfull';
export default {
name: 'FullscreenComponent',
data() {
return {
isFullscreen: false, // 用于跟蹤當(dāng)前是否處于全屏狀態(tài)
};
},
mounted() {
// 檢查screenfull是否可用,并監(jiān)聽全屏狀態(tài)變化
if (screenfull.isEnabled) {
screenfull.on('change', this.onFullscreenChange);
}
// 監(jiān)聽ESC鍵按下事件,用于退出全屏
document.addEventListener('keydown', this.handleEscKey);
},
beforeUnmount() {
// 組件卸載時(shí)移除事件監(jiān)聽
if (screenfull.isEnabled) {
screenfull.off('change', this.onFullscreenChange);
}
document.removeEventListener('keydown', this.handleEscKey);
},
methods: {
toggleFullscreen() {
// 切換全屏狀態(tài)
if (screenfull.isEnabled) {
if (this.isFullscreen) {
screenfull.exit(); // 退出全屏
} else {
screenfull.request(this.$refs.fullscreenElement); // 進(jìn)入全屏
}
} else {
alert('哎呀,你的瀏覽器不支持全屏功能哦。');
}
},
onFullscreenChange() {
// 更新全屏狀態(tài)
this.isFullscreen = screenfull.isFullscreen;
},
handleEscKey(event) {
// 按下ESC鍵時(shí)退出全屏
if (event.key === 'Escape' && this.isFullscreen) {
screenfull.exit();
}
},
},
};
</script>
<style scoped>
.fullscreen-content {
width: 100%;
height: 100%;
background-color: #f0f0f0;
padding: 20px;
box-sizing: border-box;
}
</style>
第三步:在主應(yīng)用中使用全屏組件
打開src/App.vue,引入并使用我們的全屏組件:
<template>
<div id="app">
<!-- 使用全屏組件 -->
<FullscreenComponent />
</div>
</template>
<script>
import FullscreenComponent from './components/FullscreenComponent.vue';
export default {
name: 'App',
components: {
FullscreenComponent, // 注冊(cè)全屏組件
},
};
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
第四步:運(yùn)行項(xiàng)目
一切準(zhǔn)備就緒,讓我們啟動(dòng)項(xiàng)目,看看效果吧:

打開瀏覽器,點(diǎn)擊“一鍵全屏”按鈕,見證奇跡的時(shí)刻!按下Esc鍵,全屏模式瞬間消失,仿佛什么都沒發(fā)生過。

二、原生js方案
在某些場(chǎng)景下,我們可能不需要引入額外的庫(kù),而是希望使用原生JavaScript來實(shí)現(xiàn)全屏功能。下面是一個(gè)使用原生JavaScript實(shí)現(xiàn)全屏功能的示例代碼。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"> <!-- 設(shè)置文檔字符編碼為UTF-8 -->
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- 設(shè)置視口,使其適應(yīng)設(shè)備寬度 -->
<title>Document</title> <!-- 設(shè)置文檔標(biāo)題 -->
</head>
<body>
<!-- 主內(nèi)容區(qū)域,設(shè)置樣式使其顯示為綠色、大字體、加粗 -->
<div id="main" style="color: green; font-size: 100px; font-weight: 700;">
看我看我,我是庚云!
</div>
<!-- 全屏按鈕 -->
<button id="btn">全屏</button>
<script>
// 獲取主內(nèi)容區(qū)域的DOM元素
let elem = document.getElementById('main');
// 獲取全屏按鈕的DOM元素
let btn = document.getElementById('btn');
// 為按鈕添加點(diǎn)擊事件監(jiān)聽器,點(diǎn)擊時(shí)調(diào)用requestFullScreen函數(shù)并傳入主內(nèi)容區(qū)域元素
btn.addEventListener('click', () => {
requestFullScreen(elem);
});
// 定義requestFullScreen函數(shù),用于請(qǐng)求全屏顯示指定元素
function requestFullScreen(element) {
// 嘗試獲取不同瀏覽器下的全屏請(qǐng)求方法
var requestMethod = element.requestFullScreen || element.webkitRequestFullScreen || element.mozRequestFullScreen || element.msRequestFullScreen;
// 如果存在全屏請(qǐng)求方法,則調(diào)用該方法
if (requestMethod) {
requestMethod.call(element);
} else if (typeof window.ActiveXObject !== "undefined") {
// 如果瀏覽器支持ActiveXObject(通常是IE瀏覽器),則模擬按下F11鍵進(jìn)入全屏
var wscript = new ActiveXObject("WScript.Shell");
if (wscript !== null) {
wscript.SendKeys("{F11}");
}
}
}
</script>
</body>
</html>
效果圖:


總結(jié)
通過上述代碼,我們可以實(shí)現(xiàn)一個(gè)簡(jiǎn)單的全屏功能,適用于大多數(shù)現(xiàn)代瀏覽器。
到此這篇關(guān)于基于Vue實(shí)現(xiàn)頁(yè)面全屏封裝的詳細(xì)步驟的文章就介紹到這了,更多相關(guān)Vue頁(yè)面全屏封裝內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
登錄頁(yè)面的實(shí)現(xiàn)及跳轉(zhuǎn)代碼實(shí)例(vue-router)
在Vue.js中可以使用vue-router來實(shí)現(xiàn)前端路由,通過路由來跳轉(zhuǎn)頁(yè)面,這篇文章主要給大家介紹了關(guān)于登錄頁(yè)面的實(shí)現(xiàn)及跳轉(zhuǎn)(vue-router)的相關(guān)資料,需要的朋友可以參考下2023-12-12
案例實(shí)操vue事件修飾符帶你快速了解與應(yīng)用
這篇文章主要介紹了vue常見的事件修飾符,在平時(shí)無論是面試還是學(xué)習(xí)工作中都會(huì)經(jīng)常遇到的,本文就帶你快速上手,需要的朋友可以參考下2023-03-03
vue清空數(shù)組的幾個(gè)方式(小結(jié))
本文主要介紹了vue清空數(shù)組的幾個(gè)方式,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12
vue+Ant?Design進(jìn)度條滑塊與input聯(lián)動(dòng)效果實(shí)現(xiàn)
最近接到這樣一個(gè)需求滑塊進(jìn)度與輸入框?yàn)橐恢?默認(rèn)值為80,最小不能小于30,最大為100,怎么實(shí)現(xiàn)這個(gè)聯(lián)動(dòng)效果呢,下面小編給大家分享下基于vue+Ant?Design進(jìn)度條滑塊與input聯(lián)動(dòng)效果的實(shí)現(xiàn),感興趣的朋友跟隨小編一起看看吧2022-12-12
Vue 自定義指令實(shí)現(xiàn)一鍵 Copy功能
指令 (Directives) 是帶有 v- 前綴的特殊特性。這篇文章主要介紹了Vue 自定義指令實(shí)現(xiàn)一鍵 Copy的功能,需要的朋友可以參考下2019-09-09
vue實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09

