JavaScript opacity adjustments not achieving desired outcome

My attempt to make this sub menu fade in and out upon button press is not working as expected. It should fully fade in shortly after clicking 'help' and completely fade out when clicking 'back'. Is the problem with the method I'm using to make it fade out or is it a strange bug with opacity?

function closepopup(ele) {
  ele.parentElement.style.opacity = 1;
  let interval = setInterval(() => {
    ele.parentElement.style.opacity -= 0.1;
    if(ele.parentElement.style.opacity <= 0){
      ele.parentElement.style.display = 'none';
      clearInterval(interval);
    }
  }, 50);
}

function openpopup(eleid){
  let ele = document.getElementById(eleid)
  ele.style.opacity = 0
  ele.style.display = 'flex';
  let interval = setInterval(() => {
    ele.style.opacity += 0.1;
    if(ele.style.opacity >= 1){
      clearInterval(interval);
    }
  }, 50);
}
body{
  background: #93E9BE;
}
.wrapper{
  display: flex;
  width: 100%;
  height:100%;
  align-content: center;
  justify-content: space-between;
  align-items: center;
  flex-direction:column;
}
.buttons{
  display: flex;
  width: 50%;
  height:40%;
  margin: auto;
  align-content: center;
  justify-content: space-between;
  align-items: center;
  flex-direction:column;
}
button{
  width:50%;
  height:25%;
  background:#a68e72;
  border-style:solid;
  border-color:#8a7154;
  border-radius:1vmax;
  transition: 0.5s;
  font-size:2vmax;
  color:rgba(0,0,0,0.5);
}
button:hover{
  transform:scale(1.1);
  filter:brightness(1.25);
  box-shadow:0px 0px 30px -3px #000000;
}
button:active{
  transform:scale(0.95);
  filter:brightness(0.95)
}
.popupmenu{
  display:none;
  flex-direction: column;
  position:absolute;
  top:15%;
  left:15%;
  width:70%;
  height:70%;
  background:#a68e72;
  border-style:solid;
  border-color:#8a7154;
  border-radius:1vmax;
  padding:2%;
  font-size:2vmax;
  overflow-y:auto;
  overflow-x:hidden;
}
.popupexitbutton{
  margin:auto;
  width:70%;
  height:70%;
}
<body>
<div class="wrapper">
<div class="buttons">
  <button>Play</button>
  <button>Settings</button>
  <button onclick="openpopup('help')">Help</button>
</div>
</div>
<div id="help" class="popupmenu">
  <p>
    To receive support, ask for help in the discord chat or ask a user during a match using the chat feature.
  </p>
  <button class="popupexitbutton" onclick="closepopup(this)">
    Back
  </button>
</div>
</body>

Answer №1

One issue that needs to be addressed is that ele.style.opacity is currently a string, and by adding a number to it, you are encountering problems.

A solution to this problem would be to convert ele.style.opacity into a number before performing any calculations.

ele.style.opacity = Number(ele.style.opacity) + 0.1;

See the working example provided below:

function closepopup(ele) {
  ele.parentElement.style.opacity = 1;
  let interval = setInterval(() => {
    ele.parentElement.style.opacity -= 0.1;
    if(ele.parentElement.style.opacity <= 0){
      ele.parentElement.style.display = 'none';
      clearInterval(interval);
    }
  }, 50);
}

function openpopup(eleid){
  let ele = document.getElementById(eleid)
  ele.style.opacity = 0
  ele.style.display = 'flex';
  let interval = setInterval(() => {
    ele.style.opacity = Number(ele.style.opacity) + 0.1;
    if(ele.style.opacity >= 1){
      clearInterval(interval);
    }
  }, 50);
}
/* CSS code here */
<body>
<div class="wrapper">
<div class="buttons">
  <button>Play</button>
  <button>Settings</button>
  <button onclick="openpopup('help')">Help</button>
</div>
</div>
<div id="help" class="popupmenu">
  <p>
    To receive support, ask for help in the discord or use the chat feature during a match.
  </p>
  <button class="popupexitbutton" onclick="closepopup(this)">
    Back
  </button>
</div>
</body>

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

Attempting to include a new choice on a drop-down menu and ensure its visibility within the drop-down menu

On my journey into the world of web development, I am facing a challenge with adding an option to a dropdown list. Despite pressing the add button to insert "Pasta" as a new option, it is not showing up in the list immediately. However, selecting all optio ...

An error occurred due to an unexpected identifier, '_classCallCheck', while the import call was expecting exactly one

Encountering an unexpected identifier '_classCallCheck'. Import call requires precisely one argument. Having trouble with React Native. I have attempted every solution found on the internet, but none proved successful. Is there any way to upgrade ...

Align two spans vertically in a div using Bootstrap

Struggling to create a left-hand side navigation card with Bootstrap and Bootstrap icons. The goal is to have the page name on the left and an icon on the right, but vertical alignment is posing a challenge. Take a look at the code snippet below: <b-li ...

A Foolproof Method to Dynamically Toggle HTML Checkbox in ASP.NET Using JavaScript

Although there have been numerous inquiries related to this subject, I haven't been able to find a solution specific to my situation. I currently have a gridview that contains checkboxes. What I'm trying to achieve is that when I select the chec ...

Position 2 identical classes side by side on the horizontal axis

Currently enrolled in the MIMO HTML course and facing a challenge where I am unable to align both elements with the class="column" attribute horizontally using display: inline-block; I have attempted using float:right and other CSS properties to achieve a ...

Issue with Vue.js app display within Chrome extension

I have encountered an issue while trying to run a Vuejs app in a Chrome extension as a new tab. The app renders perfectly fine when running it from the dist/ folder using simplehttpserver dist/. However, when I attempt to load the dist folder as a Chrome E ...

PHP is unable to match a CAPTCHA code with the string entered by the user

I've developed a login system along with a CAPTCHA string that randomly generates characters in the form A1B2C3, containing uppercase and lowercase letters. Below is the code snippet: $cArr1 = array_merge(range("a", "z"), range("A", "Z")); $cArr2 = r ...

Analyzing user input in PHP against a mysqli database

I'm currently working on an online quiz application. I have everything in place with the form, and it's successfully sending emails to the administrator. However, there's a specific functionality that I want to implement: I need the script ...

Alter the Cascading Style Sheets (CSS) value by accessing a

I have two files, keyboard.css and keyboard.js. My objective is to modify a CSS rule: .ui-keyboard div { font-size: 1.1em; } Is there an alternative method to change the font size without utilizing $(".ui-keyboard div").css()? I want the modification ...

Unexpected JavaScript Bug (with jQuery)

I have an interesting code snippet that captures button clicks and then hides the existing content, revealing the content of the clicked item instead. This code is part of my init.js file along with other functionalities: $(function() { $('#conta ...

Utilizing Bootstrap for aligning a span element beneath a div container

I'm working on enhancing a project by integrating more Bootstrap elements. In one part, I have a circular div with the text "Go to Homepage" below it. My goal is to center the span under the div and have it appear on a single line. How can I achieve t ...

Creating an Account with Firebase

I've created a function called signup in my web project that uses JavaScript to add a user to my Firebase database. The function works fine, but I'm encountering an issue when I try to redirect to another page at the end of the function - the use ...

JavaScript format nested data structure

For my latest project, I am working on a blog using Strapi combined with Nuxt. To fetch the categories and articles data for my blog, I send a JSON object from my front-end application using Axios. { "data": [ { "id": 1, ...

Attempting to reach <p> from numerous web pages

Currently, I am working on a project where I need to extract text data from various websites. I have successfully managed to retrieve the text data using <p> tags. I would really appreciate any assistance with this task. Thank you! ...

Is it feasible to maintain a persistent login session in Firebase without utilizing the firebase-admin package through the use of session cookies?

Currently, I am integrating Firebase into my next.js application for user login functionality. The issue I am facing is that users are getting logged out every time they switch paths within the site. Even though their session cookie has not expired, if the ...

CSS DROP DOWN NAVIGATION MENU - Struggling to maintain hover effect on main menu item while navigating through sub-menu

I am facing a challenge with a CSS-only drop-down menu where the top main menu item loses its highlight when I move the cursor over the sub-menu items. You can view the menu in action here: . For example, if you hover over "Kids" and then move your cursor ...

Steps to apply the nav-active class to a navigation using jQuery just once

Seeking assistance with a problem I am facing. I would like to add the nav-active class to an li element only once, meaning if a child element is selected, then the parent li should not have the nav-active class. Below is the code I am using, but it does n ...

What is the reason for $http.get not requiring a return value?

I am currently developing an angular application and I have a controller that interacts with a service. The controller sends a URL to the service, which then makes a GET request to that URL and receives JSON data in return. Within this JSON data is a URI f ...

Module not found in Node.js environment (webpack)

I seem to be encountering an issue with loading any modules. Despite reinstalling my operating system, I continue to face the same error when attempting to use any module. I have tried reinstalling node, clearing the cache, and more. To view the code, pl ...

Create a collection of boxes using THREE.js and save them in a 3D array

My latest project involves rendering a 16x16 grid of boxes in THREE.js using custom code. const drawGroup = () => { const blockSize = 16 // Positioning for (let x = 0; x < blockSize; x++) { for (let y = 0; y < blockSize; y++) ...