无法在“元素”上执行“requestFullScreen”:API只能由用户手势启动 [英] Failed to execute 'requestFullScreen' on 'Element': API can only be initiated by a user gesture

查看:1610
本文介绍了无法在“元素”上执行“requestFullScreen”:API只能由用户手势启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让我的HTML5游戏在发布时全屏显示。当我按下按钮时,它会全屏显示。但是当我使用window.onload使其进入全屏上载时,它会在控制台上回复无法在'Element'上执行'requestFullScreen':API只能由用户手势启动。我正在使用铬。有解决方法吗?

I want to make my HTML5 game go native fullscreen on launch. When I do this with a button onclick, it goes fullscreen. But when I use window.onload to make it go fullscreen onload, it replys on the console Failed to execute 'requestFullScreen' on 'Element': API can only be initiated by a user gesture. I am using chrome. Is there a workaround this?

我的代码:

    <!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>


推荐答案

任何javascript api都是故意以这种方式制作的。想象一下,如果你去一个网站,它会自动打开全屏,而你根本不做任何事情。你会接触到一个令人烦恼的弹出窗口和你甚至打算打开的其他东西的整个世界。因此任何此类脚本都需要用户交互才能工作。在没有用户交互的情况下切换到fullsreen非常感觉像恶意。例如,如此网站以全屏方式打开:虚拟桌面有人可能会对桌面上发生的事情感到困惑。

Any javascript api is intentionally made this way I guess. Just imagine if you go to a website and it automatically turns on in fullscreen without you doing anything at all. You would be exposed to a whole world of annoying popups and other stuff you dint even intend to open. So any such script requires user interaction to work. Switching to fullsreen without user interaction quite very much feels like malicious intent. Example if a site such as this opened up in fullscreen : Virtual Desktop someone may pretty much be confused of what just happened to their desktop.

如果您仍想尝试部分解决方案,请查看以下答案:

If you still want to try a partial workaround maybe then take a look at following answer :

Partial WorkAround

这篇关于无法在“元素”上执行“requestFullScreen”:API只能由用户手势启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆