内容 (webview) 没有填满应用程序 window

the content (webview) doesnt fill the app window

我正在尝试使用 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>

不幸的是,内容(网络视图)未调整大小以填充可用 space,它具有全宽但高度保持在 150 像素,当我在检查器面板中手动设置不同的尺寸然后它会调整。我如何告诉 webview 它应该占用完整的 space?百分比不起作用,即

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

您已将 webview 的高度设置为 100%,但没有为父项(在本例中为 body)设置明确的高度。提供 body 和明确的大小应该可以解决您的问题:

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

另请参阅:

  • Setting height: 100% on my label element doesn't work
  • Make body have 100% of the browser height