User:Stumblean/common.js: Difference between revisions
No edit summary Tags: Mobile edit Mobile web edit Advanced mobile edit |
No edit summary Tags: Mobile edit Mobile web edit Advanced mobile edit |
||
Line 3: | Line 3: | ||
const menu = document.createElement("div"); | const menu = document.createElement("div"); | ||
menu. | menu.id = "mahitoMenu"; | ||
menu.style | Object.assign(menu.style, { | ||
position: "fixed", | |||
top: "20px", | |||
right: "20px", | |||
background: "#111", | |||
color: "#fff", | |||
border: "2px solid #666", | |||
borderRadius: "10px", | |||
padding: "10px", | |||
fontFamily: "monospace", | |||
zIndex: "99999", | |||
boxShadow: "0 0 10px black", | |||
touchAction: "none", | |||
}); | |||
const header = document.createElement("div"); | const header = document.createElement("div"); | ||
Line 20: | Line 24: | ||
header.style.gap = "6px"; | header.style.gap = "6px"; | ||
header.style.marginBottom = "8px"; | header.style.marginBottom = "8px"; | ||
header.style.cursor = "grab"; | |||
const img1 = document.createElement("img"); | const img1 = document.createElement("img"); | ||
Line 59: | Line 64: | ||
menu.appendChild(btn2); | menu.appendChild(btn2); | ||
document.body.appendChild(menu); | document.body.appendChild(menu); | ||
// === DRAGGING === | |||
let isDragging = false; | |||
let offsetX = 0, offsetY = 0; | |||
function startDrag(x, y) { | |||
isDragging = true; | |||
const rect = menu.getBoundingClientRect(); | |||
offsetX = x - rect.left; | |||
offsetY = y - rect.top; | |||
menu.style.left = rect.left + "px"; | |||
menu.style.top = rect.top + "px"; | |||
menu.style.right = "auto"; // remove fixed right | |||
} | |||
function doDrag(x, y) { | |||
if (!isDragging) return; | |||
menu.style.left = x - offsetX + "px"; | |||
menu.style.top = y - offsetY + "px"; | |||
} | |||
function stopDrag() { | |||
isDragging = false; | |||
} | |||
header.addEventListener("mousedown", (e) => { | |||
startDrag(e.clientX, e.clientY); | |||
e.preventDefault(); | |||
}); | |||
document.addEventListener("mousemove", (e) => { | |||
doDrag(e.clientX, e.clientY); | |||
}); | |||
document.addEventListener("mouseup", stopDrag); | |||
header.addEventListener("touchstart", (e) => { | |||
const touch = e.touches[0]; | |||
startDrag(touch.clientX, touch.clientY); | |||
e.preventDefault(); | |||
}); | |||
document.addEventListener("touchmove", (e) => { | |||
if (!isDragging) return; | |||
const touch = e.touches[0]; | |||
doDrag(touch.clientX, touch.clientY); | |||
}); | |||
document.addEventListener("touchend", stopDrag); | |||
})(); | })(); |