javascript full screen 全屏顯示頁(yè)面元素的方法
一種最簡(jiǎn)單的方式,就是動(dòng)態(tài)改變你想要全屏顯示的部件的style,例如position變成absolute,height和width都設(shè)置成窗口大小,并且把背景顏色改成全白(為了遮住頁(yè)面上其余的元素)。這樣網(wǎng)頁(yè)上就只能看到你要突出的部件了,視覺上就等同于全屏。同時(shí)利用javascript鍵盤事件,一旦用戶按了ESc退出鍵,就恢復(fù)原來的樣子。部分代碼如下:
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.zIndex = 5;
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();
},
但是這樣做有個(gè)缺點(diǎn),就是還需要手工按一下F11來達(dá)到真正的全屏。
下面有一種方法不用自己按F11的:
<!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>
這個(gè)可以支持大部分的瀏覽器。但是討厭的IE還是不能支持HTML5的全屏功能,需要模擬按F11這個(gè)動(dòng)作。讀者可以在代碼中看到。
還可以在代碼里面退出全屏界面:
function cancelFullScreen(el) {
var requestMethod = el.cancelFullScreen||el.webkitCancelFullScreen||el.mozCancelFullScreen||el.exitFullscreen;
if (requestMethod) { // cancel full screen.
requestMethod.call(el);
} else if (typeof window.ActiveXObject !== "undefined") { // Older IE.
var wscript = new ActiveXObject("WScript.Shell");
if (wscript !== null) {
wscript.SendKeys("{F11}");
}
}
}
關(guān)于全屏顯示,我還是很好奇,那么視頻網(wǎng)站是如何做到對(duì)IE等瀏覽器都兼容的全屏功能的。如果有誰(shuí)知道的話,還請(qǐng)分享一下,感激不盡。
updated (2013/09/22)
很多時(shí)候,想在全屏切換的時(shí)候做一些自定義的事情。可以如下綁定事件:
document.addEventListener("fullscreenchange", function () {
doit();
}, false);
document.addEventListener("mozfullscreenchange", function () {
doit();
}, false);
document.addEventListener("webkitfullscreenchange", function () {
doit();
}, false);
它會(huì)在每次進(jìn)入或者退出全屏的時(shí)候,觸發(fā)doit()操作。
相關(guān)文章
JS 獲取瀏覽器和屏幕寬高等信息的實(shí)現(xiàn)思路及代碼
本節(jié)代碼主要使用了Document對(duì)象關(guān)于窗口的一些屬性,附實(shí)現(xiàn)代碼及源程序解決,有需求的朋友可以參考下2013-07-07JavaScript定時(shí)器setTimeout、setInterval使用詳解
網(wǎng)站開發(fā)過程中經(jīng)常會(huì)用到各種各樣的定時(shí)任務(wù),這時(shí)我們會(huì)用到setTimeout和setInterval方法,下面這篇文章主要給大家介紹了關(guān)于JavaScript定時(shí)器setTimeout、setInterval使用的相關(guān)資料,需要的朋友可以參考下2023-04-04屏蔽鼠標(biāo)右鍵、Ctrl+N、Shift+F10、Alt+F4、F11、F5刷新、退格鍵
js 防止刷新網(wǎng)頁(yè)、禁止后退,右鍵等操作的代碼2010-03-03GoJs中導(dǎo)出圖片或者SVG實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了GoJs中導(dǎo)出圖片或者SVG實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05javascript巧用eval函數(shù)組裝表單輸入項(xiàng)為json對(duì)象的方法
這篇文章主要介紹了javascript巧用eval函數(shù)組裝表單輸入項(xiàng)為json對(duì)象的方法,實(shí)例分析了JavaScript使用eval函數(shù)動(dòng)態(tài)構(gòu)造json對(duì)象的相關(guān)技巧,需要的朋友可以參考下2015-11-11JavaScript設(shè)置IFrame高度自適應(yīng)(兼容各主流瀏覽器)
IFrame高度的設(shè)置問題一直都是前端的噩夢(mèng)而且還要兼容各主流瀏覽器更是難上加難了,下面與大家分享下一個(gè)不錯(cuò)的技巧,感興趣的你可以參考下哈2013-06-06關(guān)于javaScript注冊(cè)click事件傳遞參數(shù)的不成功問題
在javaScript中給一個(gè)html元素注冊(cè)click事件處理函數(shù)時(shí),比如給該處理函數(shù)傳3個(gè)參數(shù)。可是不管是使用下面那種方式都不能給事件處理函數(shù)傳遞參數(shù)2014-07-07