You know the modal is visible (opacity: 1;) when it is open, and invisible (opacity: 0;) when it is closed. You can transition this opacity value to animate the opening/closing of the modal.
There’s a problem with the animation. Can you see it?
It helps to slow down the animation to debug animation-related problems. You can see the problem much better. It also helps if you changed the background-color of the modal (so it’s different from the button).
There are no problems when opening the modal. We wanted the modal to fade in, and it did.
But there’s a problem when we close the modal. The button appeared before the modal begins fading. This happened because we don’t have a transition for z-index.
This is what is happening:
When opening:
z-index changes first (this is correct)
opacity changes over 3 seconds
When closing:
z-index changes first (this is wrong)
opacity changes over 3 seconds
This is what we want:
When opening:
z-index changes first
opacity changes over 3 seconds
When closing:
opacity changes over 3 seconds
z-index changes after 3 seconds
Here’s how we can delay the z-index transition by 3 seconds:
Since we fixed the problem, remember to change the animation back to its original speed! Also, remember to change the modal’s background-color back to the original color!