Electron自定义顶部菜单

自定义顶部菜单
  1. main.js

    const { app, BrowserWindow} = require('electron')
    
    // 热加载
    const reloader = require('electron-reloader')
    reloader(module)
    
    // 监听初始化完成的生命周期
    app.on('ready', () => {
        const mainWindow = new BrowserWindow({
            width: 700,
            height: 500,
            // 创建无边框窗口
            frame: false,
            //开启渲染进程node模块和remote模块
            webPreferences: {
                nodeIntegration: true,
                enableRemoteModule: true,
                contextIsolation: false,
            }
        })
    
    	// 加载页面
        mainWindow.loadFile('./src/index.html')
    
        // 打开控制台
        // mainWindow.webContents.openDevTools()
    
    })
    
  2. index.js

    const {remote: {BrowserWindow}, shell} = require('electron')
    
    const newWindow = document.querySelector('.new-window')
    
    // 新建窗口
    newWindow.onclick = function () {
        new BrowserWindow({
            width: 300,
            height: 200
        })
    }
    
    //跳转网页到默认浏览器打开
    const allA = document.querySelectorAll('a')
    allA.forEach(item => {
        item.onclick = function (e) {
            e.preventDefault()
            shell.openExternal(item.href)
        }
    })
    
  3. index.css

    * {
        margin: 0;
        padding: 0;
    }
    
    .custom-menu {
        height: 40px;
        width: 100%;
        background-color: cornflowerblue;
        /* 支持拖动 */
        -webkit-app-region: drag;
    }
    
    .custom-menu ul {
        list-style: none;
    }
    
    .custom-menu ul li {
        float: left;
        width: 50px;
        line-height: 40px;
        text-align: center;
        margin-left: 10px;
        cursor: default;
        /* 不支持drag */
        -webkit-app-region: no-drag;
    }
    
    a {
        text-decoration: none;
        color: black;
    }
    
  4. index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>ELectron</title>
        <link rel="stylesheet" href="./css/index.css">
    </head>
    <body>
        <div class="custom-menu">
            <ul>
                <li class="new-window">文件</li>
                <li class="about">
                    <a href="http://yanggeol.top">关于</a>
                </li>
            </ul>
        </div>
        <h4>Hello YANGGEOL!</h4>
        <script src="./js/index.js"></script>
    </body>
    </html>
    
  5. 调试

    npm run electron
    
效果

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值