内容(webview)不会填满应用程序窗口

时间:2016-01-27 13:52:42

标签: javascript css webview electron

我正在试用电子来创建一个独立版本的网站,我有这个main.js(摘录)

function createWindow() {
    // Create the browser window.
    mainWindow = new BrowserWindow({
        width: 800,
        height: 600,
        "min-width": 800,
        "min-height": 500,
        resize: true,
        "use-content-size": true
    });

    // and load the index.html of the app.
    mainWindow.loadURL('file://' + __dirname + '/index.html');

    // Open the DevTools.
    mainWindow.webContents.openDevTools();
    // Set Window Resizable
    mainWindow.isResizable(true);

    // Emitted when the window is closed.
    mainWindow.on('closed', function () {
        // Dereference the window object, usually you would store windows
        // in an array if your app supports multi windows, this is the time
        // when you should delete the corresponding element.
        mainWindow = null;
    });
}

然后index.html看起来像这样:

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        body {
            margin: 0;
            padding: 0;
            background-color: #6e6e6e;
        }
        webview {
            display: block;
        }

    </style>
</head>

<body>
    <webview src="build/index.html" disablewebsecurity></webview>
</body>

</html>

遗憾的是,内容(webview)未调整大小以填充可用空间,它具有全宽但高度保持在150px,当我在检查器面板中手动设置不同的大小然后进行调整。如何告诉webview它应占用空间?百分比不起作用,即

webview {
width:100%;
height:100%;
}

我已将以下代码添加到index.html

<script>
    function onResize() {
        var height = document.body.clientHeight;
        var width = document.body.clientWidth;
        console.log("onResize", arguments, width, height);
    }
    addEventListener('resize', onResize);

</script>

输出以下结果(高度始终为150!)

onResize [Event] 216 150
index.html:27 onResize [Event] 216 150
index.html:27 onResize [Event] 1096 150
index.html:27 onResize [Event] 1096 150
index.html:27 onResize [Event] 1096 150
index.html:27 onResize [Event]                1080 150
        0: Event
            bubbles: true
            cancelBubble: false
            cancelable: false
            currentTarget: null
            defaultPrevented: fals
            eeventPhase: 0
            isTrusted: false
            isTrusted: false
            newHeight: 150
            newWidth: 1080
            path: Array[5]
            returnValue: true
            srcElement: webview
            target: webview
            timeStamp: 1453905541457
            type: "resize"__proto__: Event
            callee: onResize()
            length: 1
            Symbol(Symbol.iterator): values()
            __proto__: Object

1 个答案:

答案 0 :(得分:5)

您已将webview的高度设置为100%,但没有为父级设置显式高度(在本例中为body)。给body和显式大小可以解决您的问题:

body {
    width: 100vw;
    height: 100vh;
}

另见: