Is there a way to fix the issue of ContextMenu not appearing at the right position within a scrollable

I have a div within an iframe that needs to display a context menu when right-clicked. The div's length may exceed the visible area, making it scrollable. However, the issue I am facing is that the context menu appears in a different position than where the mouse was clicked. To view a screenshot, click here.

Answer №1

This JavaScript code allows you to retrieve the mouse position on a right-click event, and then use those coordinates to create a context menu that appears where the mouse is located.

Please note that this solution does not make use of the "add-buttons" feature as shown in your example.

document.getElementById("yourDiv").addEventListener("contextmenu", setContextMenuPosition)

function setContextMenuPosition(event) {
    event.preventDefault();
    if (event.which === 3) {
        createContextMenu(event.clientX, event.clientY);
    }
}

function createContextMenu(xCoord, yCoord) {
    let contextMenu = document.createElement("div");
    contextMenu.style.cssText = "position: fixed; top:" + xCoord + "; left: " + yCoord + ";";
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Changing Marker Color in Google Maps API

There are multiple Google Maps Markers colors based on certain conditions being TRUE or not. In addition, these Markers will change color when the mouse hovers over a division (a1,a2..ax). I want the Markers to revert back to their original color when th ...

Troubleshooting: Why is my console.log not working in JavaScript

Recently, I've been diving into the world of JavaScript, but for some reason, I can't seem to make console.log function properly. At the top of my HTML document in the head section, I added jQuery like this: <script type="text/javascript" sr ...

Disable the mousedown event in JavaScript/jQuery

During a drag and drop operation, I have set certain limits on the screen. When the draggable object passes these limits, I want it to return to its original position. The issue I am facing is that if the onmousedown event is active, the object does not r ...

What is the process for converting x and y coordinates to align with a fixed display?

When I make an API call, I am receiving X and Y coordinates in the following format: x: "-0.0120956897735595703" y: "0.147876381874084473" To display these coordinates on my minimap images, I have set their display to be absolute. The "left" and "top" pr ...

Sending images as a base64 string from a Titanium app to a Ruby on Rails web service

I am encountering an issue when trying to upload an image from an app that has been converted into a base64 string to a Ruby on Rails server. The app is developed using Titanium. However, after retrieving and decoding the image string back into an image, ...

What is the resolution if I need to utilize a property that is untyped?

Transitioning to TypeScript from plain old JavaScript is a step I'm taking because I believe it offers significant advantages. However, one drawback that has come to light is that not all attributes are typed, as I recently discovered. For instance: ...

Question about the origin of style sheet source files [*.css?ver=]

Can you explain the purpose of appending a query string to the end of a style sheet? I frequently come across examples like this: some-stylesheet.css?ver=1.2.3 Appreciate your insights. ...

Retrieving data from various endpoints using React JS

Here is the code snippet to fetch contents from my Django server: However, I also need to fetch from another URL: I am unsure how to achieve this. Any assistance would be greatly appreciated. useEffect(() => { fetch("http://127.0.0.1:8000/api/con ...

Tips for passing arguments to event handlers in React JS

While going through the React JS documentation, I came across the concept of "Lifting State Up" and I have some confusion about it. You can check out the codepen sample here: https://codepen.io/valscion/pen/jBNjja?editors=0010 In the TemperatureInput comp ...

Unable to display images - PhoneGap

I am experiencing an issue where images that are being dynamically added to my HTML display correctly in Firefox, but on all the Android devices I have tested, the images do not appear. Instead, a box with their alt text is shown. HTML: <div class="ui ...

retrieve the webpage hosted on the server

Seeking to develop a website where users can input a website URL and have it loaded from the server hosting the HTML page, rather than from the user's computer as iframe does by default. I've experimented with various techniques but all seem to l ...

Using Jquery to dynamically add or remove a class based on scrolling behavior

Can someone help me with identifying the position of an element so that when the user scrolls to it and it appears on the screen, an icon can be highlighted? Currently, the script I am using adds the class too early, closer to the bottom of the block. I w ...

Creating a Dynamic Dependent Dropdown with Jquery and Ajax in PHP

As a newbie in coding, I stumbled upon some valuable information to enhance my register form using Ajax. While the PHP files seem to be functioning correctly, I suspect that the JS file is not performing as expected. In the register form, you'll find ...

Is it possible to use coding to determine if a user has posted a link on Facebook?

For tracking Facebook shares on my site, I am currently utilizing jQuery to register each click on the share link. However, I am seeking a more precise method. Rather than just tracking clicks, I want to track the actual "shares". Is there a way to recei ...

Update a variable globally within setInterval function

Seeking assistance on showcasing the number of seconds that have elapsed using a setInterval function in JavaScript integrated within HTML. The code snippet below has been attempted, but it is only displaying 1 second and the test function appears to not ...

Navigating the reactive array in Vue 3 with finesse

Currently, I'm facing some challenges with my Vue 3 app in terms of getting things to work as expected. The main issue lies in properly accessing a reactive array. Let me provide you with the code for better comprehension: Within my global store sto ...

Forms for uploading and submitting multiple files

On my single page, I have several file upload forms that are generated in a loop. The issue is that the first file upload control works fine, but the others do not. <div> <form action="/docs/1046/UploadDocument?Id=1046&amp;propertyTypeId ...

Using the class method to handle jQuery events alters the context

Is it possible to access the class context from within a method being used as a jQuery event handler? The example below illustrates the scenario: class EventHandler { constructor() { this.msg = 'I am the event handler'; } ...

Reset input field when checkbox is deselected in React

How can I bind value={this.state.grade} to clear the input text when the checkbox is unchecked? The issue is that I am unable to modify the input field. If I were to use defaultValue, how would I go about clearing the input box? http://jsbin.com/lukewahud ...

Unlock the power of cross-origin resource sharing (CORS) on your Web Service by

I am facing an issue with my WebService (.asmx) page where I want to run functions from another webpage using ajax. The webpages I want to call these functions from are of a different origin than the web service. When I attempt to make an ajax call like th ...