由于第三种方式搞不明白,就用了第二种,结合博主的稍微改了一下,测试可以正常使用:
function makeDraggable(element) {
element = document.querySelector(element);
let dragging = false;
let mouseX = 0;
let mouseY = 0;
element.addEventListener('mousedown', (e) => {
dragging = true;
const { pageX, pageY } = e;
mouseX = pageX;
mouseY = pageY;
});
window.addEventListener('mouseup', () => {
dragging = false;
});
window.addEventListener('mousemove', (e) => {
if (dragging) {
const { pageX, pageY } = e;
const win = require('electron').remote.getCurrentWindow();
const pos = win.getPosition();
pos[0] = pos[0] + pageX - mouseX;
pos[1] = pos[1] + pageY - mouseY;
win.setPosition(pos[0], pos[1], true);
}
});
}
makeDraggable('#nav')