javascript full screen 全屏顯示頁面元素的方法
一種最簡單的方式,就是動態(tài)改變你想要全屏顯示的部件的style,例如position變成absolute,height和width都設(shè)置成窗口大小,并且把背景顏色改成全白(為了遮住頁面上其余的元素)。這樣網(wǎng)頁上就只能看到你要突出的部件了,視覺上就等同于全屏。同時利用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();
},
但是這樣做有個缺點,就是還需要手工按一下F11來達到真正的全屏。
下面有一種方法不用自己按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>
這個可以支持大部分的瀏覽器。但是討厭的IE還是不能支持HTML5的全屏功能,需要模擬按F11這個動作。讀者可以在代碼中看到。
還可以在代碼里面退出全屏界面:
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)站是如何做到對IE等瀏覽器都兼容的全屏功能的。如果有誰知道的話,還請分享一下,感激不盡。
updated (2013/09/22)
很多時候,想在全屏切換的時候做一些自定義的事情??梢匀缦陆壎ㄊ录?/p>
document.addEventListener("fullscreenchange", function () {
doit();
}, false);
document.addEventListener("mozfullscreenchange", function () {
doit();
}, false);
document.addEventListener("webkitfullscreenchange", function () {
doit();
}, false);
它會在每次進入或者退出全屏的時候,觸發(fā)doit()操作。
相關(guān)文章
小心!AngularJS結(jié)合RequireJS做文件合并壓縮的那些坑
小心!AngularJS結(jié)合RequireJS做文件合并壓縮的那些坑,大家在做文件合并壓縮的時候一定要注意,感興趣的朋友可以參考一下2016-01-01
getComputedStyle與currentStyle獲取樣式(style/class)
通過document.getElementById(element).style.xxx可以獲取元素的樣式信息但是對于通過class屬性引用的外部樣式表就獲取不到了,感興趣的朋友可以了解下2013-03-03
JavaScript數(shù)據(jù)可視化:ECharts制作地圖
這篇文章主要介紹了Echarts實現(xiàn)可視化地圖,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-08-08
uniapp開發(fā)微信小程序主包太大和vendor.js過大無法打包問題解決
最近工作一直在uniapp開發(fā)小程序這一塊,相信很多開發(fā)者都遇到過代碼體積太大無法打包的問題,這篇文章主要給大家介紹了關(guān)于uniapp開發(fā)微信小程序主包太大和vendor.js過大無法打包問題的解決辦法,需要的朋友可以參考下2023-11-11
Electron 結(jié)合 Selenium + chromedriver 
這篇文章主要介紹了Electron 結(jié)合 Selenium + chromedriver 驅(qū)動服務(wù)實現(xiàn)瀏覽器多開思路詳解,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-07-07
JavaScript String 對象常用方法總結(jié)
下面小編就為大家?guī)硪黄狫avaScript String 對象常用方法總結(jié)。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考2016-04-04

