如何使用Javascript使窗口全屏(在屏幕上伸展)

use*_*898 244 javascript fullscreen

如何使用JavaScript以一种适用于IE,Firefox和Opera的方式使访问者的浏览器全屏显示?

Tow*_*wer 272

在较新的浏览器中,例如Chrome 15,Firefox 10,Safari 5.1,IE 10,这是可能的.根据浏览器的设置,通过ActiveX也可以使用旧版IE.

这是怎么做的:

function requestFullScreen(element) {
    // Supports most browsers and their versions.
    var requestMethod = element.requestFullScreen || element.webkitRequestFullScreen || element.mozRequestFullScreen || element.msRequestFullScreen;

    if (requestMethod) { // Native full screen.
        requestMethod.call(element);
    } else if (typeof window.ActiveXObject !== "undefined") { // Older IE.
        var wscript = new ActiveXObject("WScript.Shell");
        if (wscript !== null) {
            wscript.SendKeys("{F11}");
        }
    }
}

var elem = document.body; // Make the body go full screen.
requestFullScreen(elem);
Run Code Online (Sandbox Code Playgroud)

用户显然需要首先接受全屏请求,并且不可能在页面加载时自动触发,它需要由用户触发(例如,按钮)

阅读更多:https://developer.mozilla.org/en/DOM/Using_full-screen_mode

  • 太棒了,任何退出全屏的方式? (9认同)
  • 它只能由用户触发(例如通过全屏按钮).无法在上载期间自动全屏. (6认同)
  • 目前可在Chrome 15,Firefox 10和Safari 5.1中使用.有关当前比赛状态的详细信息,请参阅[hacks.mozilla.org博客文章](http://hacks.mozilla.org/2012/01/using-the-fullscreen-api-in-web-browsers/). (3认同)
  • IE的拼写错误,应该是msRequestFullScreen,如文档http://msdn.microsoft.com/en-us/library/ie/dn265028(v=vs.85).aspx (3认同)
  • 一些东西.在IE中,这显然会忽略元素和全屏一切.如果你想要全屏,那么传递给`document.documentElement`的一切都将确保你获得正确的根元素('html'或'body').并且使用可以使用`cancelFullscreen()`来关闭它(或者再次为IE发送'F11'). (2认同)
  • 如果这个答案中包含取消功能,并且警告您出于安全原因不能让用户在全屏模式下使用键盘输入内容(除非在 chrome 中要求...),那就太棒了 (2认同)

Pet*_* O. 65

此代码还包括如何为Internet Explorer 9以及可能的旧版本以及最新版本的Google Chrome启用全屏.接受的答案也可用于其他浏览器.

var el = document.documentElement
    , rfs = // for newer Webkit and Firefox
           el.requestFullscreen
        || el.webkitRequestFullScreen
        || el.mozRequestFullScreen
        || el.msRequestFullscreen
;
if(typeof rfs!="undefined" && rfs){
  rfs.call(el);
} else if(typeof window.ActiveXObject!="undefined"){
  // for Internet Explorer
  var wscript = new ActiveXObject("WScript.Shell");
  if (wscript!=null) {
     wscript.SendKeys("{F11}");
  }
}
Run Code Online (Sandbox Code Playgroud)

资料来源:

  • 感谢"(但是,请注意,requestFullScreen"仅适用于""[...] Uvenvents和MouseEvents,例如click和keydown等","所以它不能被恶意使用".)" (2认同)

Sau*_*gin 52

这与JavaScript中的全屏一样接近:

<script type="text/javascript">
    window.onload = maxWindow;

    function maxWindow() {
        window.moveTo(0, 0);

        if (document.all) {
            top.window.resizeTo(screen.availWidth, screen.availHeight);
        }

        else if (document.layers || document.getElementById) {
            if (top.window.outerHeight < screen.availHeight || top.window.outerWidth < screen.availWidth) {
                top.window.outerHeight = screen.availHeight;
                top.window.outerWidth = screen.availWidth;
            }
        }
    }
</script> 
Run Code Online (Sandbox Code Playgroud)

  • 这是旧的.看下面的解决方案! (15认同)
  • 取决于您在选项中的JavaScript权限设置.您可以切换js控制窗口功能. (4认同)
  • 这发生在上一次网站使用这样的代码并且我没有阻止它:http://dorward.me.uk/tmp/fullscreen.jpeg (3认同)
  • 看一下webkit-fullscreen API:http://bleeding-edge-tlv.appspot.com/#28(来自#gdd2011) (2认同)

mik*_*son 22

这是进入和退出全屏模式的完整解决方案(也就是取消,退出,退出)

        function cancelFullScreen(el) {
            var requestMethod = el.cancelFullScreen||el.webkitCancelFullScreen||el.mozCancelFullScreen||el.exitFullscreen;
            if (requestMethod) { // cancel full screen.
                requestMethod.call(el);
            } else if (typeof window.ActiveXObject !== "undefined") { // Older IE.
                var wscript = new ActiveXObject("WScript.Shell");
                if (wscript !== null) {
                    wscript.SendKeys("{F11}");
                }
            }
        }

        function requestFullScreen(el) {
            // Supports most browsers and their versions.
            var requestMethod = el.requestFullScreen || el.webkitRequestFullScreen || el.mozRequestFullScreen || el.msRequestFullscreen;

            if (requestMethod) { // Native full screen.
                requestMethod.call(el);
            } else if (typeof window.ActiveXObject !== "undefined") { // Older IE.
                var wscript = new ActiveXObject("WScript.Shell");
                if (wscript !== null) {
                    wscript.SendKeys("{F11}");
                }
            }
            return false
        }

        function toggleFull() {
            var elem = document.body; // Make the body go full screen.
            var isInFullScreen = (document.fullScreenElement && document.fullScreenElement !== null) ||  (document.mozFullScreen || document.webkitIsFullScreen);

            if (isInFullScreen) {
                cancelFullScreen(document);
            } else {
                requestFullScreen(elem);
            }
            return false;
        }
Run Code Online (Sandbox Code Playgroud)

  • `msIsFullScreen` 怎么样? (3认同)
  • 规格已更改。`webkitCancelFullScreen` 现在是 `webkitExitFullscreen`。http://generatedcontent.org/post/70347573294/is-your-fullscreen-api-code-up-to-date-find-out-how-to (2认同)

Moh*_*day 12

这个功能就像一个魅力

function toggle_full_screen()
{
    if ((document.fullScreenElement && document.fullScreenElement !== null) || (!document.mozFullScreen && !document.webkitIsFullScreen))
    {
        if (document.documentElement.requestFullScreen){
            document.documentElement.requestFullScreen();
        }
        else if (document.documentElement.mozRequestFullScreen){ /* Firefox */
            document.documentElement.mozRequestFullScreen();
        }
        else if (document.documentElement.webkitRequestFullScreen){   /* Chrome, Safari & Opera */
            document.documentElement.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT);
        }
        else if (document.msRequestFullscreen){ /* IE/Edge */
            document.documentElement.msRequestFullscreen();
        }
    }
    else
    {
        if (document.cancelFullScreen){
            document.cancelFullScreen();
        }
        else if (document.mozCancelFullScreen){ /* Firefox */
            document.mozCancelFullScreen();
        }
        else if (document.webkitCancelFullScreen){   /* Chrome, Safari and Opera */
            document.webkitCancelFullScreen();
        }
        else if (document.msExitFullscreen){ /* IE/Edge */
            document.msExitFullscreen();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

要使用它只需调用:

toggle_full_screen();
Run Code Online (Sandbox Code Playgroud)


Séb*_*uel 10

您可以使用全屏API 您可以在此处查看示例

全屏API提供了一种使用用户整个屏幕呈现Web内容的简便方法.本文提供有关使用此API的信息.


小智 8

我用过这个......

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>
    <script language="JavaScript">
        function fullScreen(theURL) {
            window.open(theURL, '', 'fullscreen=yes, scrollbars=auto');
        }
        // End -->
    </script>
</head>

<body>
    <h1 style="text-align: center;">
        Open In Full Screen
    </h1>
    <div style="text-align: center;"><br>
        <a href="javascript:void(0);" onclick="fullScreen('http://google.com');">
            Open Full Screen Window
        </a>
    </div>
</body>

</html>
Run Code Online (Sandbox Code Playgroud)


小智 8

全新的html5技术 -全屏API为我们提供了一种以全屏模式呈现网页内容的简便方法.我们即将为您提供有关全屏模式的详细信息.试着想象一下使用这项技术可以获得的所有优势 - 全屏相册,视频甚至游戏.

但在我们描述这项新技术之前,我必须指出这项技术是实验性的,并得到所有主要浏览器的支持.

你可以在这里找到完整的教程: http ://www.css-jquery-design.com/2013/11/javascript-jquery-fullscreen-browser-window-html5-technology/

这是工作演示: http ://demo.web3designs.com/javascript-jquery-fullscreen-browser-window-html5-technology.htm


Jac*_*cob 6

简单的例子来自:http://www.longtailvideo.com/blog/26517/using-the-browsers-new-html5-fullscreen-capabilities/

<script type="text/javascript">
  function goFullscreen(id) {
    // Get the element that we want to take into fullscreen mode
    var element = document.getElementById(id);

    // These function will not exist in the browsers that don't support fullscreen mode yet, 
    // so we'll have to check to see if they're available before calling them.

    if (element.mozRequestFullScreen) {
      // This is how to go into fullscren mode in Firefox
      // Note the "moz" prefix, which is short for Mozilla.
      element.mozRequestFullScreen();
    } else if (element.webkitRequestFullScreen) {
      // This is how to go into fullscreen mode in Chrome and Safari
      // Both of those browsers are based on the Webkit project, hence the same prefix.
      element.webkitRequestFullScreen();
   }
   // Hooray, now we're in fullscreen mode!
  }
</script>

<img class="video_player" src="image.jpg" id="player"></img>
<button onclick="goFullscreen('player'); return false">Click Me To Go Fullscreen! (For real)</button>
Run Code Online (Sandbox Code Playgroud)


Has*_*med 6

试试screenfull.js。这是一个很好的跨浏览器解决方案,应该也适用于 Opera 浏览器。

跨浏览器使用 JavaScript 全屏 API 的简单包装器,可让您将页面或任何元素全屏显示。消除浏览器实现差异,因此您不必这样做。

演示


Dix*_*xit 5

创建功能

function toggleFullScreen() {

            if ((document.fullScreenElement && document.fullScreenElement !== null) ||
                    (!document.mozFullScreen && !document.webkitIsFullScreen)) {
             $scope.topMenuData.showSmall = true;
                if (document.documentElement.requestFullScreen) {
                    document.documentElement.requestFullScreen();
                } else if (document.documentElement.mozRequestFullScreen) {
                    document.documentElement.mozRequestFullScreen();
                } else if (document.documentElement.webkitRequestFullScreen) {
                    document.documentElement.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT);
                }
            } else {

                  $scope.topMenuData.showSmall = false;
                if (document.cancelFullScreen) {
                    document.cancelFullScreen();
                } else if (document.mozCancelFullScreen) {
                    document.mozCancelFullScreen();
                } else if (document.webkitCancelFullScreen) {
                    document.webkitCancelFullScreen();
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

在Html Put Code中

<ul class="unstyled-list fg-white">

            <li class="place-right" data-ng-if="!topMenuData.showSmall" data-ng-click="toggleFullScreen()">Full Screen</li>
            <li class="place-right" data-ng-if="topMenuData.showSmall" data-ng-click="toggleFullScreen()">Back</li>
        </ul>
Run Code Online (Sandbox Code Playgroud)