简体   繁体   English

Animate CC HTML5-切换全屏

[英]Animate CC HTML5 - toggle fullscreen

I have this code (below) on a buton to force my HTML5 game to fullscreen, but I'd like to have it turn back also with the button - right now it only works using ESC key. 我在脚下有此代码(下),可以强制我的HTML5游戏全屏显示,但是我想也通过按钮将其返回-现在仅可以使用ESC键。 Is it possbile? 有可能吗?

this.fsbtn.addEventListener("click", doFullscreen);

function doFullscreen() {
    var i;
    var elem = document.getElementById("animation_container");
    var fs = ["requestFullscreen", "webkitRequestFullscreen", "mozRequestFullScreen", "msRequestFullscreen"];

    for (i = 0; i < 4; i++) {
        if (elem[fs[i]]) {
            elem[fs[i]]();
            break;
        }
    }
}

Sure it's possible. 当然可以。 Change your doFullscreen function to a toggle one that checks if it's fullscreen or not: doFullscreen功能更改为可检查全屏功能的切换功能:

 function toggleFullscreen(event) { var element = document.body; if (event instanceof HTMLElement) { element = event; } var isFullscreen = document.webkitIsFullScreen || document.mozFullScreen || false; element.requestFullScreen = element.requestFullScreen || element.webkitRequestFullScreen || element.mozRequestFullScreen || function () { return false; }; document.cancelFullScreen = document.cancelFullScreen || document.webkitCancelFullScreen || document.mozCancelFullScreen || function () { return false; }; isFullscreen ? document.cancelFullScreen() : element.requestFullScreen(); } 

Read the documentation here for fullscreen API 在此处阅读文档以获取全屏API

You can exit from fullscreen mode using functions listed below(for more read documentation) 您可以使用下面列出的功能退出全屏模式(有关更多阅读文档)

在此处输入图片说明

In JS/HTML code you can add button with absolute position and high z-index. 在JS / HTML代码中,您可以添加具有绝对位置和高z-index的按钮。 Write new click listener for added button and run cancelFullscreen function, that's all. 编写新的单击侦听器以添加按钮并运行cancelFullscreen函数,仅此而已。

Example JS function for FullScreen mode: 全屏模式的示例JS函数:

  function toggleFullScreen() { if (!document.mozFullScreen && !document.webkitFullScreen) { if (videoElement.mozRequestFullScreen) { videoElement.mozRequestFullScreen(); } else { videoElement.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT); } } else { if (document.mozCancelFullScreen) { videoElement.mozCancelFullScreen(); } else { videoElement.webkitCancelFullScreen(); } } } 

Working example you can see here: https://developer.mozilla.org/samples/domref/fullscreen.html 您可以在这里看到工作示例: https : //developer.mozilla.org/samples/domref/fullscreen.html

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

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