0
votes

I created a context menu which successfully appear on right click. The only downside is that this context menu only appear in the top left corner. I do not get the menu at the current mouse position. I found several code snippets which I couldĀ“n append to my code. I guess there must be a simple 1-2 liner to track and set the mouseX and mouseY position. Any suggestion will help.

function contextMenu() {
    d3.event.preventDefault()
    
    var contextMenu = document.getElementById("context-menu")
        //contextMenu.style.top = ?
        //contextMenu.style.left = ?
        contextMenu.classList.add("active")

    window.addEventListener("click", function () {
        contextMenu.classList.remove("active")
    })

    document.getElementById("addObject").addEventListener("click", addNodeClicked)
}

function addNodeClicked() {
    console.log("addNodeclicked")
}
1

1 Answers

1
votes

You will need the event:

function contextMenu(event) {

Add the following two lines instead of the comment:

contextMenu.style.top = event.clientY + "px";
contextMenu.style.left = event.clientX + "px";