requestFullscreen()方法用来在全屏模式下打开一个元素。
此方法需要特定的前缀才能在不同的浏览器中工作(请参考下面的浏览器兼容性)。
使用document.exitFullscreen()方法取消全屏模式。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
</head>
<body>
<div>
<button id="fullScreen" onclick="fullscreen();">点击全屏</button>
<button id="exitFullScreen" onclick="exitFullscreen();">退出全屏</button>
</div>
</body>
</html>
<script>
function fullscreen() {
var el = document.documentElement; /* 获取(<html>)元素以全屏显示页面 */
var rfs = el.requestFullScreen || el.webkitRequestFullScreen || el.mozRequestFullScreen || el.msRequestFullScreen;//定义不同浏览器的全屏API
//执行全屏
if (typeof rfs != "undefined" && rfs) {
rfs.call(el);
} else if (typeof window.ActiveXObject != "undefined") {
var wscript = new ActiveXObject("WScript.Shell");
if (wscript != null) {
wscript.SendKeys("{F11}");
}
}
}
//退出全屏
function exitFullscreen() {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
} else if (document.webkitExitFullscreen) {
document.webkitExitFullscreen();
} else if (document.msExitFullscreen) {
document.msExitFullscreen();
}
}
//ESC可以退出全屏
</script>