加载页面时打开全屏的javascript函数

标签 javascript html

我希望在我的 HTML 文档中有一个 javascript 函数,当网页打开时,它可以直接全屏打开。

我找到了一种在此处切换按钮时进入全屏的方法:

<!DOCTYPE html>

<html>

<head>

<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">

<title>True Fullscreen in Processing.js demo</title>

<script src="processing-1.4.1.min.js"></script>

<script type="text/processing" data-processing-target="canv">

`//put processing sketch between here` 

int red = 209;
int green = 0;
int blue = 129;

void setup() {
    //size(1000, 480);
}

void draw() {

    //background(200,0,129);
    background(red, green, blue);

}

//and here

// There's probably a better way to do this, but...
// putting this javascript function in the processing sketch lets
// the sketch's values for width and height get updated on resize. 
window.onresize = function() {
    // "canv" is the canvas displaying the Processing program.
    // Make it fill the screen, or the browser window with no sliders:
    var canvElem = document.getElementById("canv");
    var newWidth = document.documentElement.clientWidth;
    var newHeight = document.documentElement.clientHeight;
    canvElem.style.position = "fixed";
    canvElem.setAttribute("width", newWidth);
    canvElem.setAttribute("height", newHeight);
    canvElem.style.top = 0 + "px";
    canvElem.style.left = 0 + "px";
    // size() is Processing, everything else 
    // in this function is javascript
    size(newWidth, newHeight);

}

</script>

<canvas id="canv">(A Processing sketch should be running right here, but, sadly, your browser does not support the HTML5 canvas tag. Try Firefox or Safari.)</canvas>

<button onclick="toggleFullScreen()">Try it</button>

<script type="text/javascript">

`//window.onload = function() { //doesn't work`

//$(function() { //doesn't work

function toggleFullScreen() {

    if ((document.fullscreenElement && document.fullscreenElement !== null) || // alternative standard method
    (!document.mozFullScreenElement && !document.webkitFullscreenElement)) { // current working methods
        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);
        }
    }
}

</script>

</body>

</html>

任何想法将不胜感激。

干杯!

最佳答案

查看全屏 API。 http://www.sitepoint.com/html5-full-screen-api/

不过,它并未得到广泛支持。 http://caniuse.com/fullscreen

关于加载页面时打开全屏的javascript函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19438834/

相关文章:

jquery 下拉选择值

python - Django 模板翻译中的复杂引号转义

php - php for循环中的div控件

html - 我们应该使用 <base href =""/> 吗?

javascript - 为什么 'use strict' 通常在 IIFE 之后(而不是在脚本的顶部)?

javascript - 图像映射;悬停在特定区域时显示文本

html - 使子 div 与自动高度父 div 一样高

javascript - 使用ajax将数据发送到php页面并获取响应并显示在字段中

javascript - 对于 Chrome 扩展,从 https 站点调用 http 站点时出现错误

javascript - 云函数: Checking if email exists in users document in Firestore not working