var contextMenu = document.querySelector(".wrapperfortheclick"); | |
window.addEventListener("contextmenu", e => { | |
e.preventDefault(); | |
let x = e.offsetX, y = e.offsetY, | |
winWidth = document.body.scrollWidth, | |
winHeight = document.body.scrollHeight, | |
cmWidth = contextMenu.offsetWidth, | |
cmHeight = contextMenu.offsetHeight; | |
x = x > winWidth - cmWidth ? winWidth - cmWidth : x; | |
y = y > winHeight - cmHeight ? winHeight - cmHeight : y; | |
contextMenu.style.left = `${x}px`; | |
contextMenu.style.top = `${y}px`; | |
contextMenu.style.visibility = "visible"; | |
}); | |
document.addEventListener("click", () => contextMenu.style.visibility = "hidden"); | |