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

Is it possible for jQuery to create two separate variables with identical names?

When it comes to declaring variables in jQuery, you have 2 different options: The jQuery method The JavaScript method Are these two statements equivalent: $variable = "string"; And: var variable = "string"; Or do they create separate variables? ...

I'm new to learning JavaScript and I'm wondering how I can receive a single alert using only the if operator

Extracted from the book "Beginning JS 4th edition", this code snippet displays two alert messages when loaded in a browser due to two NaN entries in an array. To ensure that only one alert is shown every time, how can I achieve this using the if operator? ...

What is the alternative method of invoking a function within another function in React Native without utilizing a constructor?

Within my RegisterTaster function, I need to execute another function called endRegisterAlert. Since I'm not using a constructor and instead treating the class as a const in React Native, how can I achieve this? What is the best way to call the endRe ...

Tips for Utilizing Border-Radius in Safari

What is the best way to hide a child element's corners within its parent (#div1) Ensuring that the child does not overflow its parent container? ...

Update an existing JSON document

Looking for a solution to update the json file while retaining specific strings and values? const fs = require('fs'); var logPath = 'log.json' var logRead = fs.readFileSync(logPath) var logFile = JSON.parse(logRead) LogChannel = &apo ...

Modify the standard localStorage format

I'm encountering a dilemma with my two applications, located at mysite.com/app1 and mysite.com/app2. Both of these apps utilize similar localStorage keys, which are stored directly under the domain "mysite.com" in browsers. This setup results in the l ...

Is there a way in jQuery to identify if a paragraph contains exactly one hyperlink and no other content?

I need to assign the 'solo' class to a link within a paragraph only if that link is the sole element in the paragraph. This example would have the 'solo' class: <p><a>I am alone</a></p> However, this example w ...

Encountering an issue with npm installation following a recent node version upgrade

Having trouble installing Sass on Node version v16.14.0. I keep receiving this error message: https://i.stack.imgur.com/6KNcF.png ...

What is the best way to create a gallery using Bootstrap 4?

I'm currently working on a project for my homework that involves replicating a car brand page, but I've hit a roadblock at this particular section. https://i.stack.imgur.com/0Zfr5.jpg My goal is to recreate the gallery using a .container with n ...

Utilizing TypeScript Variables within a Jquery Each Iteration

I have a variable named tableIndexNumber that I need to use in different methods. When trying to access this variable, I use "this.tableIndexNumber" and it works fine. However, I face an issue when using it inside a jQuery each loop because the HTML elemen ...

Transferring a Variable from Arduino to JavaScript

Is there a simple way to pass an Arduino variable as a JS variable? Let's say we have an integer Arduino variable called sensorData: int sensorData = analogRead(sensorPin); How can we transfer this value to a JavaScript variable? client.println(&quo ...

Is it possible to determine which child element is currently in view within a scrollable parent div?

In an attempt to replicate a "current page" feature using divs, similar to a PDF reader. document.addEventListener("DOMContentLoaded", function(event) { var container = document.getElementById("container"); container.onscroll = function() { let ...

An Exploration into the Error Situations of NPM Request Library

I'm encountering an issue with the callback in my request function. I'm trying to figure out the specific circumstances under which an error is passed to this callback. const req = require('request'); req('http://www.google.com&ap ...

It seems that using the SVG <mask> tag is necessary for Firefox to display correctly, but it causes issues with CSS mask in

I need assistance with masking content using an external SVG image. Currently, I'm using the following code: #homepage-banner .desktop-slider.masked { -webkit-mask:url(news-panel-mask.svg) left top no-repeat; /* Chrome/Safari (Webkit) */ mask: ur ...

Is the presence of the selenium webdriver being registered?

Is there a method to detect if a website can tell I am using a bot while running Selenium WebDriver in Python or Puppeteer in JavaScript? Are there any resources that indicate whether a bot test would be failed, such as Cloudflare or Captcha? Appreciate a ...

Is there a way to prevent the DOM from loading images until Angular has successfully injected the correct variables?

Having some trouble with this block of code I have: <div class="image" ng-repeat="image in images"> <img src="{{image.url}}"></img> </div> It seems that the image sources are being set correctly, but I keep getting an error wh ...

Creating a simple bootstrap code for developing plugins in Wordpress

After successfully coding in Brackets with the Theseus plugin on my local machine, I encountered a problem when attempting to transfer my code to a Wordpress installation. The transition from Brackets to Wordpress seems far more complicated than expected. ...

Go all the way down to see the latest messages

I have developed a messaging system using Vue. The latest messages are displayed from bottom to top. I want to automatically scroll to the end of a conversation when it is clicked and all the messages have been loaded via axios. Conversation Messages Comp ...

Is it possible to encounter a MongoDB error for the $or operator in a correctly formatted query?

Here is the problem I am facing: const users = this.mongo.db.collection('Users') let query = { '$or': [ { "email": {'$eq': req.body.email }}, {"username": {'$eq': req.body.username }} ] } users.fi ...

The Line Break <br/> element does not display properly in Java Servlet code

I recently developed a dynamic web application using servlet. The main component is a form on the index.html page that triggers the servlet's doGet method. Check out an excerpt of my code for the doGet Method below. String firstName = request.getParam ...