The context menu materializes the instant the cursor hovers over it

The right-click context menu keeps appearing at the top of the page before clicking, even though I have set display: none in the CSS.

$(document).on('mousedown', function(event) {
  if (event.which == 3) {
    $('#context').css({
      top: event.pageY,
      left: event.pageX
    });
    $('#context').show();
  }
});
#context {
  display: inline-block;
  position: absolute;
  background: #fff;
  box-shadow: 2px 2px 15px rgba(0, 0, 0, 0.5);
  display: none;
}

.context-menu {
  list-style: none;
  padding: 0px;
  margin: 0px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="context">
  <ul class="context-menu">
    <li> <a href="#">Back</a></li>
    <li> <a href="#">Forward</a></li>
    <li> <a href="#">Save As...</a></li>
    <li> <a href="#">Reload</a></li>
    <li> <a href="#">Inspect Element</a></li>
  </ul>
</div>

Answer №1

const frame = document.querySelector('#frame');

const displayFrame = (x, y) => {
  frame.style.transform = `translate(${x}px, ${y}px)`;
  frame.classList.add('active');
};
const hideFrame = () => {
  frame.classList.remove('active');
};

document.addEventListener('mousedown', (e) => {
  const isVisible = frame.classList.contains('active');
  const targetClosestElement = e.target.closest(`#${frame.id}`);

  if (targetClosestElement) return;

  if (e.which === 3 && !isVisible)
    return displayFrame(e.pageX, e.pageY);

  if (e.which !== 3 && isVisible)
    hideFrame();
});

// Disable frame menu
document.addEventListener('contextmenu', e => e.preventDefault());
#frame {
  display: inline-block;
  position: absolute;
  background: #fff;
  box-shadow: 2px 2px 15px rgba(0, 0, 0, 0.5);
  visibility: hidden;
  opacity: 0;
  transition: visibility 0s .5s, opacity .5s;
}

#frame.active {
visibility: visible;
opacity: 1;
 transition: visibility 0s, opacity .5s;
}

.frame-menu {
  list-style: none;
  padding: 0px;
  margin: 0px;
}
<div id="frame">
  <ul class="frame-menu">
    <li> <a href="#">Back</a></li>
    <li> <a href="#">Forward</a></li>
    <li> <a href="#">Save As...</a></li>
    <li> <a href="#">Reload</a></li>
    <li> <a href="#">Inspect Element</a></li>
  </ul>
</div>

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

Tips for adjusting the dimensions of an SVG background within an element using the gulp-svg-sprite npm package

My current project involves the use of an NPM package called gulp-svg-sprite, which consolidates all my SVG images into a single file named sprites.svg and also generates sprites.css. Within the sprites.css file, there are CSS classes that define their ba ...

A guide on performing CRUD operations on locally stored JSON data using React JS

Retrieve the JSON data from any endpoint and store it locally fetch("any endpoint") .then((response) => response.json()) .then((responseJson) => { this.state ={ data:responseJson } }) How to perform CRUD operations on a sample JSO ...

What are some ways to customize the appearance of Stripe credit card form fields?

I've successfully implemented stripe using this HTML code and Bootstrap 4. <div class="form-control" id="card-element"></div> As a result, I get the displayed output: https://i.sstatic.net/Aw653.png However, I am loo ...

HTML anchor tag failing to redirect to specified link

I need to populate the URI property from an API result into an HTML format. I attempted to achieve this by calling a function and running a loop 3 times in order to display 3 links with the URI property of each object. However, the code is not functioning ...

Is it possible to set up the material-ui datepicker to allow for the selection of dates spanning across multiple months without losing visibility of the current month?

Is it possible to choose dates within the same calendar week, regardless of whether they fall in different calendar months? For instance, selecting Friday from the previous month when the new month begins on a Saturday. To illustrate my point, let's ...

What is the best way to align the button correctly beside the cluster of checkboxes within bootstrap framework?

Here is a snippet of HTML code that I am working with: <div class="container"> <form name ="queryForm"> <div class="form-group"> <p class="checkbox-inline"> <input type="checkbox" name="optionsRadios" id="checkOne" ...

What are the signs indicating that I've reached the threads limit set in Node.js?

My current configuration includes a thread pool size of 25. process.env.UV_THREADPOOL_SIZE = 25; How can I verify at runtime that all the threads have been used up? Is there a method to detect when all defined threads have been utilized when a new reque ...

Challenges arising from incorporating a nested for loop with table output in JavaScript

var data = { "categories":[ "Early Years (RA)", "Primary schools (RA)", "Secondary schools (RA)", "Special schls and alter prov (RA)", "Post-16 provision (RA)", "Other edu and community budget (RA)", "TOTAL EDUC ...

Clicking on buttons in the header does not lead to the intended section when scrolling

I have just completed a website project for a Udemy course, following all the instructions provided. However, I am facing an issue with the functionality of two buttons on my site. I want these buttons to scroll to specific sections when clicked. The "I&ap ...

Images appear unaligned in group on mobile view due to responsiveness

Currently facing two issues: creating a responsive container for images where they stay side by side when the resolution changes. However, on testing with a phone, the images stack vertically instead of horizontally, and one image's width appears dist ...

Create a JSON file on the fly

I am in need of performing a post request using a JSON file. The current structure of the JSON is as follows: { "compositeRequest" : [{ // Account "method" : "POST", "url" : &quo ...

substituting the deep watcher in Angular

In my application, I am working with a data object called fulldata, which consists of an array of objects. fulldata = [ {'key': 'abc', values: {.....},....}, {'key': 'efg', values: ...

I'm confused as to why my alert is not triggering after the ajax request successfully returns

I am having an issue with displaying an alert to my user before the page is submitted when making an AJAX call to a controller. Despite successfully sending parameters, the alert is not showing up. Can someone please take a look at the code below and off ...

Calculating totals in real-time with JavaScript for a dynamic PHP and HTML form that includes quantity,

I have created a dynamic form with fields and a table that adjusts based on the number of results. Each column in the form represents name, quantity, price, and total price. The PHP and SQL code runs in a loop to populate the list depending on the number ...

Executing Javascript Function Once ASP.Net Server Processing Finishes

If I have a basic web application built in asp.net/vb.net, with a single button on the web page. When this button is clicked, it triggers some functionality in the code behind. What I want to achieve is to run a JavaScript function after the page reloads, ...

Change object values to capital letters

Upon retrieving myObject with JSON.stringify, I am now looking to convert all the values (excluding keys) to uppercase. In TypeScript, what would be the best way to accomplish this? myObj = { "name":"John", "age":30, "cars": [ { "name":"Ford", ...

Opening the dropdown menu in IE8's HTML select requires a double-click

Below is the code snippet I am referring to: <!doctype html> <html lang="en"> <head> <meta charset="utf-8" /> <script src="http://code.jquery.com/jquery-1.7.1.js"></script> <style type="te ...

The dynamic value feature in Material UI React's MenuItem is currently experiencing functionality issues

Whenever I use Select in Material UI for React, I encounter an issue where I always receive undefined when selecting from the drop-down menu. This problem seems to occur specifically when utilizing dynamic values with the MenuItem component. If I switch to ...

Incorporate a Vue.js computed property into the data retrieved from a server

Coming from Knockout.js, where observables are easily created by defining them, I'm curious if there's a similar approach in Vue.js. let vm = { someOtherVar: ko.observable(7), entries: ko.observableArray() }; function addServerDataToEnt ...

Dealing with pesky table cell padding issues in Chrome

Struggling with table cell paddings in Chrome because it appears that if a table cell doesn't have pure text, the padding isn't applied. Here's my code: <table> <tr> <th></th> <th ...