Mirakurun Mirakurun - 14 days ago 6
CSS Question

Modal doesn't play animation twice on Firefox

I have a modal to view pictures which plays the animation only the first time on Firefox. When I open it first time, it works, but when I open it the second time, it doesn't play the animation. It works on Chrome tho.

HTML

<img src="https://s-media-cache-ak0.pinimg.com/236x/e8/2e/cc/e82ecc7ea98248bfa8161dcfcef2974a.jpg" id="postimage"></img>

<div id="myModal" class="modal">
<span class="close" onclick="document.getElementById('myModal').style.display='none'">&times;</span>
<img class="modal-content" id="modalImage">
</div>


CSS

.modal {
display: none;
position: fixed;
z-index: 999999;
top: 0;
left: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgba(0,0,0,0.8);
}
#postimage:hover {
opacity: 0.8;
cursor: pointer;
}

.modal-content {
display: block;
width: 100%;
margin: auto;
max-width: 720px;
}

@-webkit-keyframes fadeIn {
from {opacity:0.0}
to {opacity:1}
}

@keyframes fadeIn {
from {opacity:0.0}
to {opacity:1}
}

.modal-content {
-webkit-animation-name: fadeIn;
-webkit-animation-duration: 1.8s;
animation-name: fadeIn;
animation-duration: 1.8s;
}
.close {
position: absolute;
top: 50px;
right: 50px;
color: #e3e3e3;
font-size: 40px;
font-weight: bold;
transition: 0.3s;
}

.close:hover,
.close:focus {
cursor: pointer;
color: #787878;
}


JS

var modal = document.getElementById('myModal');

var img = document.getElementById('postimage');
var modalImg = document.getElementById("modalImage");
img.onclick = function(){
modal.style.display = "block";
modalImg.src = this.src;
}

var span = document.getElementsByClassName("close")[0];

span.onclick = function() {
modal.style.display = "none";
}
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}


JSFIDDLE

Answer

Try adding:

@-moz-keyframes fadeIn {
  from {
    opacity: 0.0;
  }
  to {
    opacity: 1;
  }
}

and updating the .modal-content with:

  -moz-animation-name: fadeIn;
  -moz-animation-duration: 1.8s;
  -moz-transition-timing-function: ease-in;
  -moz-animation-iteration-count: 1;
  -moz-animation-fill-mode: both;

-moz- is specifically for Firefox as is -webkit- is for Chrome or Opera. Found this on here

transition-timing-function effects acceleration of the animation so the speed can adjust with over various duration

animation-iteration-count is the number of times an animation cycle will play then stop

animation-fill-mode effects how the animation applies styles to the target