What is causing the extra space at the top of my page from the contextual menu?

My goal is to implement a contextual menu for my HTML web application. Strangely, when the menu is hidden, there is no space at the top of the page, but once it's activated, some empty space appears.

Contextual menu in disabled state: https://i.sstatic.net/MEUmr.png

Contextual menu in enabled state: https://i.sstatic.net/UUNPd.png

Below is the code snippet:

<style>
    #contexmenu{
        display: none;
        position: relative;
        width: 100px;
        background: rgb(238, 238, 238);}
        #contexmenu section{
            padding: 5px;}
        #contexmenu section:hover{
            background-color: rgb(219, 219, 219)}
    #mainarea{
        width: 100vw;
        height: calc(100vh - 50px);
        background: red;
    }
</style>

<article id="contexmenu">
    <section onclick="addJob()">Add Job</section>
    <section>Edit Job</section>
    <section>Refresh</section>
</article>

<article id="mainarea">

</article>

<script>

function addJob(){
    alert("");
}

document.addEventListener("contextmenu", function(event){
    event.preventDefault();
    var contexmenu = document.getElementById("contexmenu")
    contexmenu.style.display = "block";
    contexmenu.style.top = `${event.screenY - 50 - contexmenu.clientHeight}px`;
    contexmenu.style.left = `${event.screenX - 65}px`;
    console.log("New event");
})

document.addEventListener("click", function(event){
    document.getElementById("contexmenu").style.display = "none";
})
</script>

I am trying to identify what is causing this issue and figure out the necessary steps to resolve it. Any insights or suggestions would be appreciated.

Answer №1

Here is my solution

I made a modification by adding position:fixed; to the CSS selector #contexmenu

Make sure to run the code snippet in fullscreen mode for optimal viewing

function addJob() {
  alert("");
}

document.addEventListener("contextmenu", function(event) {
  event.preventDefault();
  var contexmenu = document.getElementById("contexmenu")
  contexmenu.style.display = "block";
  contexmenu.style.top = `${event.screenY - 50 - contexmenu.clientHeight}px`;
  contexmenu.style.left = `${event.screenX - 65}px`;
  console.log("New event");
})

document.addEventListener("click", function(event) {
  document.getElementById("contexmenu").style.display = "none";
})
#contexmenu {
  display: none;
  position: relative;
  width: 100px;
  background: rgb(238, 238, 238);
  position: fixed;
}

#contexmenu section {
  padding: 5px;
}

#contexmenu section:hover {
  background-color: rgb(219, 219, 219)
}

#mainarea {
  width: 100vw;
  height: calc(100vh - 50px);
  background: red;
}
<article id="contexmenu">
  <section onclick="addJob()">Add Job</section>
  <section>Edit Job</section>
  <section>Refresh</section>
</article>

<article id="mainarea">

</article>

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

What is the best way to extract several form elements and assign a new attribute to each of them?

<form name="myform"> <fieldset> <legend>Delivery Information</legend> <p>Country: <input pattern="^[A-Za-z]" type="text" name="textInput" size=&qu ...

Listen for the modified value in a directive with AngularJS

I am currently working on implementing a directive that can draw a chart based on specified values. What I am aiming for is to pass the data necessary for the plot directly from the template rather than using ng-model, as my current solution requires. I ...

Infinite scrolling dropdown feature in the <Select> widget

I currently have around 20 typical HTML select widgets on my page. Each of these selects contains a large number of elements, for example: <select> <option>1</option> ... <option>3000</option> </select> As ...

Guide to adding a new Vue-Grid-Item with a button

Currently, I am working on a software project for the Research department at my university, specifically focusing on an Atomic Layer Deposition system. The main goal of this program is to allow users to create and customize their own 'recipe' for ...

Turn off the authentication middleware for a particular HTTP method on a specific endpoint

Currently, I am using Express/Node and have developed authentication middleware to validate JWT on each request. My requirement is to disable this middleware for a specific route (POST '/api/user/') while keeping it active for another route (GET ...

JavaScript client unable to establish WebSocket connection with server

I have exhausted all tutorials from various sources, including StackOverflow, but none of them seem to resolve my issue. My problem lies in establishing a connection between a client and a server using WebSockets; I always encounter the error message WebSo ...

Calculate the average color of the outer pixels in an image

I am looking to dynamically set the background color of a div using the average color of the outer pixels in an image. This way, I can avoid manually setting the background color for each image. For example, if the image is 100px x 100px, I would check th ...

Guide to aligning the footer content at the center using the Bootstrap 4 grid structure

I am currently working on the footer design of a website and my goal is to center the contents within it. You can view the fiddle for reference. As of now, the contents in the fiddle are not perfectly centered. Here are the CSS styles I have applied to po ...

Error encountered in AngularJS and JSON server: [$injector:unpr] An unidentified provider was found: $resourceProvider <- $resource <- menuFactory

Hello everyone, I recently created a small AngularJS application and utilized a JSON server for my backend operations. Unfortunately, I am encountering an issue with the provider in my code. Upon running it, I am receiving errors as shown below: Uncaugh ...

Converting Python Class Structures into JavaScript

I currently have some Python classes structured like this: class Customer { constructor(name, fullName, gender, age, instruments, paid) { this.name = name; this.fullName = fullName; this.gender= gender; this.age= age; ...

Using Angular 2 (Typescript) to export variables from a separate file

Within my Node.js application, I have a file named variables.js. This file contains variables that are used throughout the application in one central location: exports.var1= 'a'; exports.var2= 'b'; By requiring this file in another pa ...

The input field cannot be accessed via touch on mobile devices

<div class="form-group row pswrd" style="padding: 0px 10px"> <div id="email" class="col-md-12 col-xs-12"> <input type="password" class="form-control c_fname" id="c" #pswd name="password" placeholder="password" [(ngModel)]="user.passwor ...

Creating a file solely for route definitions and then employing them within index.js

My index.js file contains the following routes: import { createRouter, createWebHistory } from '@ionic/vue-router'; import { RouteRecordRaw } from 'vue-router'; const routes = [{ path: '', redirect ...

Border concealed by Bootstrap Glyphicons

Is there a reason why glyphicons hide a border of a floated container? Can anyone shed light on this issue and suggest a workaround? Check out this link for more information. https://i.sstatic.net/CXZSd.jpg My setup: OS X 10.11.1, Chrome 47.0.2526.73 (6 ...

Using the power of Selenium's XPath, we can easily navigate through a table to access

Looking for assistance in creating selenium xpath for the various column links within a table. Each row has a unique identifier and contains information about a product, including the name. Based on the product name, I need to locate other links within the ...

Is there a way to convert a Blob object to a JSON object in Javascript?

let dataFile = new Blob([response.data], {type: 'application/json'}); Within the response.data is a JSON file. My goal is to extract the contents of this file and set them as a JSON string to a variable in JavaScript. I've experimented wit ...

"Incorporate a new dropdown menu and then append the selected item to an array within

I am trying to enhance my user interface by adding two new select boxes (from and until) dynamically using ng-click, but I am facing challenges in binding these selects to my array in the scope. Here is the HTML code snippet: <div ng-repeat="time in a ...

Creating 3D extrusions with three.js using image files as shape definitions

How can I replace the hearts with the leaf provided? I'm working on a web app that involves falling objects using Three.JS. I began with a heart shape, but now I need to swap it out with an autumn leaf (that's the only change needed). This is t ...

Having trouble with Gulp JS and CSS minification where existing minified files cannot be replaced

I've hit a roadblock in trying to resolve my search and google issue. My current challenge involves Gulp and cssmin. I can't seem to pinpoint the cause of an error. My aim is to have both the original CSS files and their minified versions in the ...

Is the <style> tag effective when placed within other elements?

When I look at it, I notice something along these lines: <div> <style type="text/css"> ... </style> </div> It seems weird, but it's functional. Does this go against any standards? ...