加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
preload.js 1.48 KB
一键复制 编辑 原始数据 按行查看 历史
JackWhh 提交于 2022-11-18 18:03 . init
const electron = require('@electron/remote')
const win = electron.getCurrentWindow();
// 鼠标按下还是抬起
let dragging = false;
let mouseX = 0;
let mouseY = 0;
window.addEventListener('DOMContentLoaded', () => {
const replaceText = (selector, text) => {
const element = document.getElementById(selector)
if (element) element.innerText = text
}
for (const dependency of ['chrome', 'node', 'electron']) {
replaceText(`${dependency}-version`, process.versions[dependency])
}
// 设置拖拽功能
const el = document.getElementById("imgAvatar");
el.addEventListener('mousedown', (e) => {
dragging = true;
const { pageX, pageY } = e;
mouseX = pageX;
mouseY = pageY;
});
})
// 鼠标抬起禁止拖拽
window.addEventListener('mouseup', () => {
dragging = false;
});
// 设置拖拽功能
window.addEventListener('mousemove', (e) => {
console.log(dragging)
if (dragging) {
const { pageX, pageY } = e;
const pos = win.getPosition();
pos[0] = pos[0] + pageX - mouseX;
pos[1] = pos[1] + pageY - mouseY;
win.setPosition(pos[0], pos[1], true);
}
});
// 设置指定区域的鼠标点击不穿透
window.addEventListener("mousemove", event => {
const el = document.getElementById("imgAvatar");
let flag = event.target === el;
if (!flag) {
win.setIgnoreMouseEvents(true, {forward: true});
} else {
win.setIgnoreMouseEvents(false);
}
});
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化