JS 實現部分區域全螢幕即退出
1. 首先找出要全螢幕區域
`var elem = document.getElementById("game");`
2. 建立全螢幕 function
function requestFullScreen(element) { var requestMethod = element.requestFullScreen || element.webkitRequestFullScreen || element.mozRequestFullScreen || element.msRequestFullScreen; if (requestMethod) { requestMethod.call(element); } else if (typeof window.ActiveXObject !== "undefined") { var wscript = new ActiveXObject("WScript.Shell"); if (wscript !== null) { wscript.SendKeys("{F11}"); } } }
|
3. 將選到 element 區塊全螢幕
`requestFullScreen(elem);`
4. 退出全螢幕函數
function exitFullscreen() { if (document.exitFullscreen) { document.exitFullscreen(); } else if (document.msExitFullscreen) { document.msExitFullscreen(); } else if (document.mozCancelFullScreen) { document.mozCancelFullScreen(); } else if (document.webkitExitFullscreen) { document.webkitExitFullscreen(); } }
|
5. 監聽按鈕事件
我是設置一個是否為全螢幕的變數去跑,這樣就只需要一個按鈕就可以解決
var btn = document.getElementById("btn"); btn.onclick = function () { var elem = document.getElementById("content"); requestFullScreen(elem); };
var close = document.getElementById("close"); close.onclick = function () { exitFullscreen(); };
---
var fullscreen = false; var btn = document.getElementById("btn"); var elem = document.getElementById("content"); btn.onclick = function () { if(fullscreen){ exitFullscreen(); fullscreen = false; }else{ requestFullScreen(elem); fullscreen = true; } };
|