I created a modal image project similar to this.
When I click on the image I want to display and scale up the modal image, and it should scale down and disappear when I click the close button. However, when I use the display property for hiding and displaying the modal image, the transform: scale
property doesn't work, and the modal image will be displayed and hidden suddenly.
I commented out document.getElementById("modalimg").style.display = "none";
which did improve things, but not enough.
I also tried to run this with the visibility property instead of the display property and it worked well - but I want to know what the problem is.
I also searched for similar questions, and found this and this, but they didn't help.
Note: the code snippet will need to be run in fullscreen to display properly.
document.getElementById("container").onclick = modal;
function modal(buttFlag) {
document.getElementById("modalimg").style.display = "inline";
document.getElementById("modalbackground").style.display = "block";
document.getElementById("butt").style.display = "block";
document.getElementById("modalimg").style.transition = "all 1s";
document.getElementById("modalimg").style.transform = "scale(7,4)";
if (buttFlag == true) {
document.getElementById("modalimg").style.transform = "scale(1,1)";
document.getElementById("modalimg").style.display = "none";//I tried comment this line and it worked better but not good enough.
document.getElementById("butt").style.display = "none";
document.getElementById("modalbackground").style.display = "none";
buttFlag = false;
}
}
#container{
width: 20%; height: 50%;
margin: 70%; margin-top: 12%;
position: absolute;
}
#container img{
width: 100%; height: 100%;
object-fit: cover;
}
#modalbackground{
position: absolute;
width: 100%;height: 100%;
background-color: black;
opacity: 0.7;
display: none;
z-index: +1;
}
#modalimg{
width: 100px; height: 100px;
position: absolute;
top:45%; left: 45%;
z-index: +1;
display: none;
}
button{
font-size: 2em;
background-color: transparent;
border: none;
position: absolute;
left: 85%; top: 5%;
color: white;
z-index: +1;
display: none;
}
<div id="modalbackground"></div>
<div id="container">
<img src="https://st4.depositphotos.com/11639344/22517/i/600/depositphotos_225179058-stock-photo-asian-rainforest-jungle-august.jpg" alt="jungle">
</div>
<button onclick="modal(true)" id="butt">☒</button>
<img id="modalimg" src="https://st4.depositphotos.com/11639344/22517/i/600/depositphotos_225179058-stock-photo-asian-rainforest-jungle-august.jpg" alt="jungle">