Transition effects are in full effect when the modal is being opened, however, they seem to be malfunctioning

Check out my sandbox here where I have demonstrated an issue. There is a button that triggers a modal to open on click. The problem is that the modal fades in slowly over 0.7 seconds, but when I try to close it by clicking the close button, it instantly disappears instead of gradually fading out like it should.

Take a look at my files:

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Static Template</title>
    <link rel="stylesheet" href="./index.css" />
  </head>
  <body>
    <div class="modal-overlay"></div>
    <div class="modal">
      This is Modal

      <button class="close-button">
        Close Modal
      </button>
    </div>

    <button class="open-button">
      Open Modal
    </button>

    <script src="./index.js"></script>
  </body>
</html>

index.css

.modal-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: #444;
  z-index: 1;
  transition: visibility 0s, opacity 0.5s;
  opacity: 0;
  visibility: hidden;
}

.modal {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 2;
  margin: auto;
  background: #fff;
  width: 350px;
  padding: 20px;
  height: 30vh;
  width: 40vw;

  visibility: hidden;
  opacity: 0;
  transition: visibility 0s, opacity 0.7s;
}

index.js

const openBtn = document.querySelector(".open-button");
const closeBtn = document.querySelector(".close-button");
const modal = document.querySelector(".modal");
const overlay = document.querySelector(".modal-overlay");

openBtn.addEventListener("click", () => {
  modal.style.visibility = "visible";
  modal.style.opacity = 1;

  overlay.style.visibility = "visible";
  overlay.style.opacity = 0.6;
});

closeBtn.addEventListener("click", () => {
  modal.style.visibility = "hidden";
  modal.style.opacity = 0;

  overlay.style.visibility = "hidden";
  overlay.style.opacity = 0;
});

Thank you for any assistance provided!

Answer №1

Never mind, it's all good now... I just added a timer to control visibility. I originally set it to 0s because I thought you couldn't transition visibility, but it turns out you can...

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

Ever thought about harnessing the power of SassDoc to create detailed documentation for your sass

After experimenting with sassdoc for documenting our sass mixins and functions, I realized that there doesn't seem to be a straightforward way to generate documentation for our sass variables. Specifically, I am looking for a solution that can output ...

Switch out div elements for td elements in HTML code

I have written a code snippet that successfully toggles between showing and hiding content. Here is the code: Simple collapsible Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliq ...

Instead of setting a fixed value, vary the background color dynamically

def dynamic_color(value, loop_name, breakpoints, metrics, symbol = ''): facts = [(round(item)) for item in value] metrics += '''<tr><th>'''+ loop_name +'''</th>''& ...

How can I determine the Windows service pack version directly from my web browser?

Is there a method to determine the installed service pack from the browser? It doesn't seem to be available in System.Web.HttpBrowserCapabilities in asp.net. I am looking for a solution to alert users about the necessity of updating to XP Service Pack ...

I need help figuring out how to databind HTML elements in my data table

I have a column in my data table that includes the value: <strong>Hello World</strong> What is the best way to bind this data to my GridView so that it displays as bold text instead of showing the word "strong"? ...

Ways to align the navbar-brand at the center using bootstrap

I have attempted multiple techniques such as flex boxes, auto margins, and transforms to center this navbar-brand element on the page. However, it continues to align between the right side and a button. I want it to disregard the button positioning and ins ...

What is preventing me from passing str.indexOf into the map function in JavaScript?

How come I'm unable to perform the following action? let str = "A string containing * and ^ as well as $"; let indices = ["*", "^", "$"].map(str.indexOf); // not functional I prefer not to introduce another function that duplicates the functionality ...

Exploring the possibilities of NodeJs by analyzing an HTML form

I'm facing an issue while evaluating an HTML Form using NodeJs and Express. Here's my JavaScript Code My aim is to manage HTML Forms in NodeJs using Express. const express = require('express'); const http = require('http'); ...

Tips for retrieving the value when a button is clicked in AngularJS

Looking for assistance on retrieving the selected value in AngularJS when a button is clicked. Below is the code I am currently using: <div class="form-inline"> <div class="form-group"> <select class="form-control" data-n ...

Separate sentence to one word per line

Can CSS be used to display each word of a sentence on a separate line? Input: <div>Hello world foo bar</div> Rendered output: Hello world foo bar It is not ideal to set the width to 1px, as specified. ...

Is it possible to rearrange an array by moving an element to the front

If I have an array as shown below, how can I move key [2] and its corresponding value to the front of the array? This would make it key [0] and increment the other keys by 1. Current: [0] => Array ( [name] => Vanilla Coke cans 355ml x 2 ...

Updating the background color and adjusting the main content background on a WordPress site

After spending nearly an hour searching for a solution to my problem with no luck, I've come to the conclusion that most YouTube videos are not helpful at all. My website is built on WordPress and I am attempting to change the background color and mai ...

Node.js Mistake - 'Attempting to import outside of a module is not allowed'

I’m currently immersed in a React project, and my focus is... Firebase.js import firebase from 'firebase/app' import 'firebase/database' import 'firebase/auth' const firebaseConfig = { apiKey: process.env.FIREBASE_AP ...

Incorporating a personalized image to create custom icons on the Material UI bottom navigation bar

Is it possible to replace the default icon with a custom image in material ui's BottomNavigation component? I'm curious if Material UI supports this feature. If you'd like to take a closer look, here is the link to the codesandbox. ...

I am having trouble with selecting an element from a JavaScript webpage using Python's scrapy and Selenium

I am in the process of developing an application to collect data. My tools of choice are Python 2.7 with Scrapy and Selenium on Windows 10. I have successfully implemented this on a few web pages before, however, I am facing an issue with selecting or clic ...

The CSS child selector seems to be malfunctioning as it is affecting all descendants of the specified type

Struggling with creating a menu containing nested lists. Attempted using a child selector (#menu-novo li:hover > ul) to display only immediate descendants, but all are still showing. Any suggestions or solutions for this issue? #menu-novo-container { ...

The process of setting up a carousel for tables using jQuery

I can successfully implement jquery for an image carousel, but now I need to find a way to create a sliding table format. Any assistance on this matter would be greatly appreciated. Here is the code I currently have: <ul> <li><a style= ...

Vertical text area expansion functioning in Chrome, but not in Firefox

Is there a way to create a textarea that dynamically expands to fill the available space within a div, while maintaining fixed height offsets from the top and bottom? The CSS code below achieves this effect in Chrome, but Firefox displays the textarea with ...

Updating vertices in the RingGeometry using THREE.js

Is there a way to dynamically update the vertices of a RingGeometry in THREE.JS? Previously, I achieved this by creating a new RingGeometry and replacing the old one's vertices with those of the new geometry. This method worked perfectly in version 6 ...

Specify the title attribute by using a language-based string

Can the title attribute content on an image be defined as a language string that can be overridden in a multilingual website? I am considering following this format: <img title="tooltip_mod".....> Creating a tooltip_mod INI file that defi ...