点击按钮全屏

时间:2014-12-20 12:50:27

标签: javascript fullscreen

我想有一个onclick按钮使页面全屏显示(如模拟F11按下)

例如:

<body>
<button onclick="fullscreen()">Click to go fullscreen</button>
</body>

1 个答案:

答案 0 :(得分:2)

试试这个......

<script>
function fullscreen() {
    if ((document.fullScreenElement && document.fullScreenElement !== null) ||
        (!document.mozFullScreen && !document.webkitIsFullScreen)) {
        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 {
        if (document.cancelFullScreen) {
            document.cancelFullScreen();
        } else if (document.mozCancelFullScreen) {
            document.mozCancelFullScreen();
        } else if (document.webkitCancelFullScreen) {
            document.webkitCancelFullScreen();
        }
    }
</script>
<button onclick="fullscreen()">Click to go fullscreen</button>