简体   繁体   English

JavaScript / HTML5全屏API

[英]JavaScript/HTML5 FullScreen API

<div id="divbody">
    <button id="begin">Click me</button>

    <script>
        $(document).ready(function() {
            $("#begin").click(function() {
                var e = document.getElementById('divbody');
                e.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT);
            });
            document.addEventListener("webkitfullscreenchange",function(){
                if (document.webkitIsFullScreen) {
                    //alert('a');
                    document.webkitCancelFullScreen();
                }
            }, false);
        });
    </script>
</div>

The following code basically should cancel full screen as soon as it enters. 以下代码基本上应该在进入后立即取消全屏显示。 However, the code above does not work (eg, it enters full screen but does not cancel back). 但是,上面的代码不起作用(例如,它进入全屏显示但不会取消返回)。 However, by uncommenting the alert in the webkitfullscreenchange event handler, it does actually cancel. 但是,通过取消注释webkitfullscreenchange事件处理程序中的警报,它实际上可以取消。

I have hard time understanding why this is so. 我很难理解为什么会这样。 Also, how would I achieve what I am trying to do without using alert? 另外,如何在不使用警报的情况下实现我要执行的操作?

Thanks. 谢谢。

UPDATE 更新

I have tried all the comments, but it does not seem to work. 我已经尝试了所有评论,但似乎没有用。 Any help on this would be greatly appreciated! 任何帮助,将不胜感激!

Questions like this where an alert() fixes a problem is always a matter of the sequence of events. 诸如alert()解决问题的问题总是与事件顺序有关。 One solution that almost always works is to put the offending code in a short timing function: 几乎总是可行的一种解决方案是将有问题的代码放在较短的计时函数中:

window.setTimeout(cancelFull,10);
function cancelFull() { document.webkitCancelFullScreen(); }

UPDATE Put the setTimeout() in place of your current CancelFullScreen, inside the listener. UPDATE将setTimeout()放在侦听器中,以替换当前的CancelFullScreen。

尝试这个:

window.setTimeout(document.webkitCancelFullScreen, 10);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM