HTML5,javascript如何让浏览器自动全屏显示(或者其它方法也可以)
<html >
<meta charset="UTF-8">
<body>
<button onclick="launchFullscreen(document.documentElement);">启动全屏</button>
<script>
function launchFullscreen(element) {
if(element.requestFullscreen) {
element.requestFullscreen();
} else if(element.mozRequestFullScreen) {
element.mozRequestFullScreen();
} else if(element.webkitRequestFullscreen) {
element.webkitRequestFullscreen();
} else if(element.msRequestFullscreen) {
element.msRequestFullscreen();
}
}
</script>
</body>
</html>
现在这段代码只能点击按钮才能全屏,有没有什么方法自动全屏浏览器的
请给出代码实现
谢谢
豆芽Dsoul
9 years, 10 months ago
Answers
我试着改写成立即执行的函数表达式:
(function launchFullscreen(element) {
if(element.requestFullscreen) {
element.requestFullscreen();
} else if(element.mozRequestFullScreen) {
element.mozRequestFullScreen();
} else if(element.webkitRequestFullscreen) {
element.webkitRequestFullscreen();
} else if(element.msRequestFullscreen) {
element.msRequestFullscreen();
}
})(document.documentElement);
但是chrome提示:
Failed to execute 'requestFullScreen' on 'Element': API can only be initiated by a user gesture.
貌似意设定:这种api操作只能有人来出发。。。
api给的例子也是由一个条件来出发这个html5的全屏函数:
element.requestFullScreen()
Makes an individual element full-screen, e.g. document.getElementById(“myvideo”).requestFullScreen().
悲剧D团子
answered 9 years, 10 months ago