想把某個區塊「放大到佔滿整個畫面」,其實有幾種不同的思路。本文整理從純 CSS 模擬到瀏覽器 Fullscreen API 的多種方案,並附上監聽全螢幕切換事件的方法。
最單純的方式,是動態修改目標元素的 style:把 position 改成 absolute,寬高設成視窗大小,再把背景設為白色以遮住其他元素,視覺上就等同於全屏。同時監聽鍵盤事件,當使用者按下 Esc 時還原成原本的樣子。
document.onkeydown = function (event) {
var e = event || window.event || arguments.callee.caller.arguments[0];
if (e && e.keyCode == 27) { //ESC鍵
$('.navbar-inner').fadeIn(100);
var maintable = document.getElementById("holder");
maintable.style.position = "relative";
maintable.style.height = "100%";
maintable.style.width = "100%";
maintable = document.getElementById("main");
maintable.style.height = "100%";
maintable.style.width = "100%";
maintable.style.left = 0 + "px";
maintable.style.top = 0 + "px";
resizePlots();
}
};
fullScreenClick: function () {
$('.navbar-inner').fadeOut(100);
var maintable = document.getElementById("holder");
maintable.style.position = "absolute";
maintable.style.background = "#fff";
maintable.style.height = $(window).height() + "px";
maintable.style.width = $(window).width() + "px";
maintable.style.left = 0 + "px";
maintable.style.top = 0 + "px";
maintable = document.getElementById("main");
maintable.style.height = "90%";
maintable.style.width = "90%";
maintable.style.left = $(window).width() * 0.05 + "px";
maintable.style.top = $(window).height() * 0.02 + "px";
resizePlots();
},
這種做法的缺點是:它只是「看起來」像全屏,仍需要使用者手動按 F11 才能進入瀏覽器真正的全螢幕。
如果不想讓使用者手動按 F11,可以改用瀏覽器的全螢幕 API。下面的範例點擊按鈕後,會讓指定元素進入全螢幕。
<!DOCTYPE html>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<body>
<button id="btn"> full screen </button>
<div id="content" style="height:500px;width:500px;background:#fff">
<h1>歡迎微博互粉!</h1>
<h2>weibo.com/leavingseason</h2>
<p>相信音樂,相信五月天</p>
</div>
</body>
<script language="JavaScript">
document.getElementById("btn").onclick=function(){
var elem = document.getElementById("content");
requestFullScreen(elem);
};
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}");
}
}
}
</script>
</html>
這段程式針對各家瀏覽器做了 prefix 相容處理(webkit、moz、ms)。值得一提的是,舊版 IE 不支援 HTML5 全螢幕功能,因此程式會用 ActiveXObject 模擬按下 F11 這個動作來達到效果。
相對應地,也可以透過程式主動退出全屏:
function cancelFullScreen(el) {
var requestMethod = el.cancelFullScreen||el.webkitCancelFullScreen||el.mozCancelFullScreen||el.exitFullscreen;
if (requestMethod) {
requestMethod.call(el);
} else if (typeof window.ActiveXObject !== "undefined") {
var wscript = new ActiveXObject("WScript.Shell");
if (wscript !== null) {
wscript.SendKeys("{F11}");
}
}
}
如果想在進入或退出全屏時執行自訂行為(例如重繪圖表),可以綁定 fullscreenchange 事件:
document.addEventListener("fullscreenchange", function () {
doit();
}, false);
document.addEventListener("mozfullscreenchange", function () {
doit();
}, false);
document.addEventListener("webkitfullscreenchange", function () {
doit();
}, false);
以上三個事件會在每次進入或退出全屏時觸發 doit(),方便你同步調整版面或重繪內容。