我正在开发一个电子应用程序,我需要创建一个自定义标题栏。设计已经完成,并且理论上可以正常工作(使用JS中的element.click()测试),但是CSS中的:hover和JS onclick事件均无效。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>test</title>
<style>
html,
body {
margin: 0;
height: 100%;
overflow: hidden;
background: #131313;
}
#titlebar {
width: 100%;
height: 16px;
max-height: 16px;
-webkit-user-select: none;
-webkit-app-region: drag;
font-family: sans-serif;
font-weight: 600;
color: white;
padding: 10px 5px;
}
#titlebar-buttons {
display: inline-block;
position: absolute;
right: 0px;
top: 0px;
}
.titlebar-button {
display: inline-block;
height: 16px;
width: 32px;
text-align: center;
padding: 8px;
z-index: 1;
}
.titlebar-button:hover {
background: #232323;
}
#webview {
width: 100%;
height: calc(100% - 32px);
}
</style>
<script>
const {
remote
} = require('electron')
let mainWindow = remote.BrowserWindow.getFocusedWindow()
let currentWindow
</script>
</head>
<body>
<div id="titlebar">
<span id="titlebar-title"></span>
<div id="titlebar-buttons">
<span id="titlebar-minimize" class="titlebar-button" onclick="mainWindow.minimize();">_</span>
<span id="titlebar-maximize" class="titlebar-button" onclick="mainWindow.isMaximized() ? mainWindow.unmaximize() : mainWindow.maximize()"><img width="10" height="10" id="maximize-icon"></span>
<span id="titlebar-close" class="titlebar-button" onclick="mainWindow.close();">X</span>
</div>
</div>
<webview id="webview" src="https://example.com/"></webview>
<script>
webview.addEventListener('dom-ready', () => {
//webview.openDevTools()
setInterval(function() {
document.querySelector('#titlebar-title').innerText = webview.getTitle()
document.querySelector('title').innerText = webview.getTitle()
currentWindow = remote.BrowserWindow.getFocusedWindow()
document.querySelector('#maximize-icon').src = mainWindow.isMaximized() ? "data:image/svg+xml;utf8;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iaXNvLTg4NTktMSI/Pgo8IS0tIEdlbmVyYXRvcjogQWRvYmUgSWxsdXN0cmF0b3IgMTkuMC4wLCBTVkcgRXhwb3J0IFBsdWctSW4gLiBTVkcg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