我想让我的 HTML5 游戏在启动时全屏显示。当我通过单击按钮执行此操作时,它会全屏显示。但是当我使用 window.onload 使其进入全屏加载时,它在控制台上回复 Failed to execute 'requestFullScreen' on 'Element': API 只能由用户手势启动。我正在使用铬。有解决方法吗?
我的代码:
<!DOCTYPE html>
<html>
<head>
<script>
window.onload = openfullscreen();
function launchIntoFullscreen(element) {
if(element.requestFullscreen) {
element.requestFullscreen();
} else if(element.mozRequestFullScreen) {
element.mozRequestFullScreen();
} else if(element.webkitRequestFullscreen) {
element.webkitRequestFullscreen();
} else if(element.msRequestFullscreen) {
element.msRequestFullscreen();
}
}
function openfullscreen() {
launchIntoFullscreen(document.documentElement);
}
function exitFullscreen() {
if(document.exitFullscreen) {
document.exitFullscreen();
} else if(document.mozCancelFullScreen) {
document.mozCancelFullScreen();
} else if(document.webkitExitFullscreen) {
document.webkitExitFullscreen();
}
window.close();
}
function fix() {
var screenwidth = screen.width;
var screenhei = screen.height;
document.getElementById('ifam').width = screenwidth;
document.getElementById('ifam').height = screenhei;
}
</script>
<style>
#ifam {
position:fixed;
left:0%;
top:0%;
z-index:-1;
}
#fullscreen {
position:fixed;
left:0%;
top:0%;
z-index:1;
}
</style>
</head>
<body onload="fix()">
<div id="fullscreen">
<button onclick="openfullscreen()">Open</button>
<button onclick="exitFullscreen()">Exit</button>
</div>
<iframe id="ifam" src="lchosser.html"></iframe>
</body>
</html>