我第一次對網頁全屏模式有概念,是那種網頁播放視頻的全屏播 放的那種。感受很強,前幾個星期有個需求也是關於全屏模式的,接觸以後才知道全屏模式並不神祕,是個很容易掌握的技能...javascript
博客、 前端積累文檔、 公衆號、 GitHub
進去看看,玩一下,本文將結合這個demo一塊兒進行講解。html
我把全屏模式封裝在一個類裏面,在代碼中有詳細的註釋,若是有須要的話,直接把類拿出來,根據栗子和註釋使用便可。前端
代碼在codepen的demo裏。java
MDN介紹:git
使用提供的API,讓一個元素與其子元素,能夠佔據整個屏幕,並在此期間,從屏幕上隱藏全部的瀏覽器用戶界面以及其餘應用。github
chrome下的全屏表現:web
在這種狀況下退出全屏,只會退出紅色全屏,退回到左邊全屏的形式,因此頁面依然是全屏模式。chrome
Esc
或調用退出全屏方法,退出全屏。標籤欄和書籤欄依然是隱藏的,網頁上的元素恢復成本來的尺寸。要顯示書籤欄和標籤欄,須要刷新一下頁面。api
總共用到6個API:
document.fullscreenEnabled
Element.requestFullscreen()
document.exitFullscreen()
document.fullscreenElement
document.fullscreenchange
document.fullscreenerror
目前並非全部的瀏覽器都實現了API的無前綴版本,因此咱們須要針對不一樣瀏覽器,作一下API的兼容:
這是我在demo中作的瀏覽器兼容:
/** * @description: 是否支持全屏+判斷瀏覽器前綴 * @param {Function} fn 不支持全屏的回調函數 這裏設了一個默認值 */ isFullscreen(fn) { let fullscreenEnabled; // 判斷瀏覽器前綴 if (document.fullscreenEnabled) { fullscreenEnabled = document.fullscreenEnabled; } else if (document.webkitFullscreenEnabled) { fullscreenEnabled = document.webkitFullscreenEnabled; this.prefixName = 'webkit'; } else if (document.mozFullScreenEnabled) { fullscreenEnabled = document.mozFullScreenEnabled; this.prefixName = 'moz'; } else if (document.msFullscreenEnabled) { fullscreenEnabled = document.msFullscreenEnabled; this.prefixName = 'ms'; } if (!fullscreenEnabled) { if (fn !== undefined) fn(); // 執行不支持全屏的回調 this.isFullscreenData = false; } }
我在實例化的時候進行一次判斷瀏覽器是否支持全屏,而後保存瀏覽器前綴。
推薦這麼作,由於若是每一個API都要這樣重複的判斷瀏覽器前綴,那也太噁心了!
document.fullscreenEnabled
屬性返回一個布爾值,表示當前文檔是否能夠切換到全屏狀態。
代碼在上方瀏覽器前綴代碼中給出了。
若是沒有保存瀏覽器前綴的話,注意作一下不一樣瀏覽器前綴的兼容!下面再也不強調。
/** * @description: 將傳進來的元素全屏 * @param {String} domName 要全屏的dom名稱 */ Fullscreen(domName) { const element = document.querySelector(domName); // 獲取dom const methodName = this.prefixName === '' ? 'requestFullscreen' : `${this.prefixName}RequestFullScreen`; // API前綴 element[methodName](); // 調用全屏 }
這就是咱們實現全屏的API,是否是超簡單?
值得注意的是:調用此API並不能保證元素必定可以進入全屏模式
<iframe>
元素具備 allowfullscreen 屬性,可選擇是否將其內容以全屏模式顯示這種不被容許全屏的元素屬於極少數狀況,我試過能夠將button
全屏。
點擊事件等
)中調用,不然將會被拒絕。在demo中有演示,初始化直接全屏,會觸發進入全屏失敗回調。
介紹:
exitFullscreen() { const methodName = this.prefixName === '' ? 'exitFullscreen' : `${this.prefixName}ExitFullscreen`; // API 前綴 document[methodName](); // 調用 }
調用這個方法會讓文檔回退到上一個調用Element.requestFullscreen()方法進入全屏模式以前的狀態。
多層全屏
像demo中,先進入左邊全屏,再進入紅色全屏,即爲:多層全屏的狀況(雖然這種狀況並很少)。
當出現多層全屏的狀況,須要一層層的退出到頁面最初始的狀況,並非調用一次document.exitFullscreen()
就恢復到頁面最初始的樣子。
fullscreenElement屬性返回正處於全屏狀態的Element節點,若是當前沒有節點處於全屏狀態,則返回null
/** * @description: 檢測有沒有元素處於全屏狀態 * @return 布爾值 */ isElementFullScreen() { const fullscreenElement = document.fullscreenElement || document.msFullscreenElement || document.mozFullScreenElement || document.webkitFullscreenElement; // 有前綴的f是大寫,沒前綴是小寫 if (fullscreenElement === null) { return false; // 當前沒有元素在全屏狀態 } else { return true; // 有元素在全屏狀態 } }
事實上,還有一個屬性document.fullscreen
,返回一個布爾值,表示文檔是否處於全屏模式。
兩個方法效果是同樣,但由於IE不支持這個屬性,因此這裏用的是document.fullscreenElement
當咱們進入全屏和離開全屏的時候,都會觸發一個fullscreenchange
事件。
MDN注意:此事件不會提供任何信息,代表是進入全屏或退出全屏。
看了很久事件返回的信息,確實找不到一個值,代表這是在進入全屏,或者離開全屏!
能夠說至關不人性化了!但咱們能夠經過檢查當前是否有節點處於全屏狀態,判斷當前是否處於全屏模式。
/** * @description: 監聽進入/離開全屏 * @param {Function} enter 進入全屏的回調 * @param {Function} quit 離開全屏的回調 */ screenChange(enter,quit) { if (!this.isFullscreenData) return; const methodName = `on${this.prefixName}fullscreenchange`; document[methodName] = e => { if (this.isElementFullScreen()) { enter && enter(e); // 進入全屏回調 } else { quit && quit(e); // 離開全屏的回調 } }; }
注意:多層全屏的狀況
進入全屏並不老是成功的,多是技術緣由,也多是用戶拒絕,咱們在上文進入全文的APIElement.requestFullscreen()
部分講過了。
好比全屏請求不是在事件處理函數中調用,會在這裏攔截到錯誤
/** * @description: 瀏覽器沒法進入全屏時觸發 * @param {Function} enterErrorFn 回調 */ screenError(enterErrorFn) { const methodName = `on${this.prefixName}fullscreenerror`; document[methodName] = e => { enterErrorFn && enterErrorFn(e) }; }
chorme 70 下的默認會爲正在全屏的dom添加兩個class:稍微看一下
:not(:root):-webkit-full-screen::backdrop { position: fixed; top: 0px; right: 0px; bottom: 0px; left: 0px; background: black; // 會將背景設爲黑色的 若是你沒爲你的dom設置背景的話,全屏下會爲黑色 }
:not(:root):-webkit-full-screen { object-fit: contain; position: fixed !important; top: 0px !important; right: 0px !important; bottom: 0px !important; left: 0px !important; box-sizing: border-box !important; min-width: 0px !important; max-width: none !important; min-height: 0px !important; max-height: none !important; width: 100% !important; height: 100% !important; transform: none !important; margin: 0px !important; }
全屏狀態的CSS:
全屏狀態下,大多數瀏覽器的CSS支持:full-screen僞類,只有IE11支持:fullscreen僞類。使用這個僞類,能夠對全屏狀態設置單獨的CSS屬性。
如下css摘自阮一峯老師的Fullscreen API:全屏操做
/* 針對dom的全屏設置 */ .div:-webkit-full-screen { background: #fff; } /* 全屏屬性 */ :-webkit-full-screen {} :-moz-full-screen {} :-ms-fullscreen {} /* 全屏僞類 當前chrome:70 不支持 */ :full-screen { } :fullscreen { /* IE11支持 */ }
咱們能夠把全屏技術應用在H5遊戲、信息流網站、視頻等地方,下次再有全屏需求時,記住不要慌,回頭看看過本文的栗子,把我封裝的類拿出來直接用就能夠啦!
以上2018.12.1
參考資料: