Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

247
Views
transition effects works opening modall, but not working when modal is closing

I made a sandbox here to demonstrate my problem. I have a button that opens a modal on click. You can see that the modal slowly shifts from opacity 0 to opacity 1 during 0.7sec. But when I clock the close button on the modal it disappears instantly instead of slowly disappearing within 0.7sec like when opening it.

Here are 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;
});

Thanks for the help

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

nvm, it works now... just put timer in visibility... I placed it to 0s because I read that you can't transition visibility but I guess you can..

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!