想實現的需求:
1、網頁上有個“全屏顯示”按鈕(鏈接),點擊全屏後執行函數fullScreen(),然後“顯示全屏”二字變成“退出全屏”;
2、點擊“退出全屏”執行函數exitFullScreen(),然後“退出全屏”變為“全屏顯示”
function fullScreen() {
var el = document.documentElement;
var rfs = el.requestFullScreen || el.webkitRequestFullScreen ||
el.mozRequestFullScreen || el.msRequestFullScreen;
if(typeof rfs != "undefined" && rfs) {
rfs.call(el);
} else if(typeof window.ActiveXObject != "undefined") {
//for IE,這裡其實就是模擬了按下鍵盤的F11,使浏覽器全屏
var wscript = new ActiveXObject("WScript.Shell");
if(wscript != null) {
wscript.SendKeys("{F11}");
}
}
}
function exitFullScreen() {
var el = document;
var cfs = el.cancelFullScreen || el.webkitCancelFullScreen ||
el.mozCancelFullScreen || el.exitFullScreen;
if(typeof cfs != "undefined" && cfs) {
cfs.call(el);
} else if(typeof window.ActiveXObject != "undefined") {
//for IE,這裡和fullScreen相同,模擬按下F11鍵退出全屏
var wscript = new ActiveXObject("WScript.Shell");
if(wscript != null) {
wscript.SendKeys("{F11}");
}
}
}
<a href="#" onclick="return ExecuteFun(this)">全屏顯示</a>
<script>
function ExecuteFun(a) {
var full = a.innerHTML == '全屏顯示';
full ? fullScreen() : exitFullScreen();
a.innerHTML = full ? '退出全屏' : '全屏顯示';
return false;
}
function fullScreen() {
var el = document.documentElement;
var rfs = el.requestFullScreen || el.webkitRequestFullScreen ||
el.mozRequestFullScreen || el.msRequestFullScreen;
if (typeof rfs != "undefined" && rfs) {
rfs.call(el);
} else if (typeof window.ActiveXObject != "undefined") {
//for IE,這裡其實就是模擬了按下鍵盤的F11,使浏覽器全屏
var wscript = new ActiveXObject("WScript.Shell");
if (wscript != null) {
wscript.SendKeys("{F11}");
}
}
}
function exitFullScreen() {
var el = document;
var cfs = el.cancelFullScreen || el.webkitCancelFullScreen ||
el.mozCancelFullScreen || el.exitFullScreen;
if (typeof cfs != "undefined" && cfs) {
cfs.call(el);
} else if (typeof window.ActiveXObject != "undefined") {
//for IE,這裡和fullScreen相同,模擬按下F11鍵退出全屏
var wscript = new ActiveXObject("WScript.Shell");
if (wscript != null) {
wscript.SendKeys("{F11}");
}
}
}
</script>