通过按钮将网站切换到持久全屏模式

Switching website into persistent full screen mode with a button

本文关键字:模式 按钮 网站      更新时间:2023-09-26

我正在努力在我的网站上实现两个目标:
1.切换到全屏模式的按钮
2.在页面之间传递时,将保持全屏模式。

我能够单独实现这两个目标,但不能一起实现
目前的情况是:
1.按钮使用以下代码切换到全屏模式:

<script>
       function toggleFullScreen() {
           if (!document.fullscreenElement &&    // alternative standard method
               !document.mozFullScreenElement && !document.webkitFullscreenElement && !document.msFullscreenElement ) {  // current working methods
             if (document.documentElement.requestFullscreen) {
               document.documentElement.requestFullscreen();
             } else if (document.documentElement.msRequestFullscreen) {
               document.documentElement.msRequestFullscreen();
             } else if (document.documentElement.mozRequestFullScreen) {
               document.documentElement.mozRequestFullScreen();
             } else if (document.documentElement.webkitRequestFullscreen) {
               document.documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
             }
           } else {
             if (document.exitFullscreen) {
               document.exitFullscreen();
             } else if (document.msExitFullscreen) {
               document.msExitFullscreen();
             } else if (document.mozCancelFullScreen) {
               document.mozCancelFullScreen();
             } else if (document.webkitExitFullscreen) {
               document.webkitExitFullscreen();
             }
           }
         }
       </script>
      <li class="quicklinks"> <a href="#" class="layout-condensed-fullwidth" id="layout-condensed-width" >
        <div onclick="toggleFullScreen();" class="iconset top-menu-fullpage-dark"></div>
        </a> </li>

但是,当切换页面时,全屏模式将退出!

  1. 当通过点击F11(而不是如上所述)切换到全屏模式时,我能够使用以下代码使全屏持久:

            <script>
                function fullscreen(){
                $('a').click(function() {
                if(!$(this).hasClass('noeffect')) {
                    window.location = $(this).attr('href');
                    return false;
                }
            });
            $(document).ready(function() {
                fullscreen();
            });
            </script>
    

但我找不到将这两者结合起来的方法。第2节中的代码只有在按F11时才有效。关于为什么会发生这种情况以及什么可以帮助解决这个问题,有什么想法吗?

谢谢!

根据MDN-使用全屏模式在全屏模式下导航到另一个页面、更改选项卡或切换到另一应用程序(例如使用Alt Tab)退出全屏模式

然而,我在全屏API包装器中看到了一个技巧/破解来解决这个问题。它使用iframe可以在全屏状态下切换页面。也许你可以使用这个包装器或者从代码中寻找灵感。请参阅演示。

这里是代码片段:

        // a little hack to be able to switch pages while in fullscreen.
        // we basically just creates a seamless iframe and navigate in that instead.
        $('#iframe').click(function () {
            // We create an iframe and fill the window with it
            var iframe = document.createElement('iframe')
            iframe.setAttribute('id', 'external-iframe');
            iframe.setAttribute('src', 'http://bbc.com');
            iframe.setAttribute('frameborder', 'no');
            iframe.style.position = 'absolute';
            iframe.style.top = '0';
            iframe.style.right = '0';
            iframe.style.bottom = '0';
            iframe.style.left = '0';
            iframe.style.width = '100%';
            iframe.style.height = '100%';
            $('#container').prepend(iframe);
            document.body.style.overflow = 'hidden';
        })

也许您可以将iframe.src更改为链接href,而不是更改window.location