dangelsaurus
dangelsaurus

Reputation: 7532

CSS Animations only applied after bootstrap modal is shown

I've noticed that when using Bootstrap Modal, if I have CSS Animations for elements inside my modal, those animations aren't applied until after the modal is displayed. This is actually great, but I can't figure out what "triggers" the animations. what's more, if I close the modal and re-open. the animations are reset and run again.

I'm pretty sure this is a Bootstrap thing, but I can't find any documentation specifically about this.

The reason I'm interested in this, is that I'd like to take advantage of this by re-running the animations. and it would make sense to try to hook into an existing Bootstrap function if possible. (that, and I'm just curious)

.animate-box {
  width: 100px;
  height: 100px;
  background: red;
  position: relative;
  animation-name: example;
  animation-duration: 3s;
  animation-delay: 0s;
  animation-fill-mode: both;
}

@keyframes example {
  from {
    background-color: yellow;
  }
  to {
    background-color: blue;
  }
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">


<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalLong">
  Launch demo modal
</button>


<!-- Modal -->
<div class="modal fade" id="exampleModalLong" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-body">
        <div class="animate-box">
        </div>
      </div>
    </div>
  </div>
</div>


<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

Upvotes: 0

Views: 447

Answers (1)

Kosh
Kosh

Reputation: 18393

See CSS Animations Level 1 (kudos to @TemaniAfif for the link)

Setting the display property to none will terminate any running animation applied to the element and its descendants. If an element has a display of none, updating display to a value other than none will start all animations applied to the element by the animation-name property, as well as all animations applied to descendants with display other than none.

input + label:before {content:'Show'}
input:checked + label:before {content:'Hide'}

input:checked ~ .animate-box {display:block}


.animate-box {
  display: none;
  width: 100px;
  height: 100px;
  background-color: yellow;
  animation: example 3s both;
}

@keyframes example {
  to {
    background-color: blue;
  }
}
<input type="checkbox" id="sw">
<label for="sw"></label>

<div class="animate-box">
</div>

Upvotes: 1

Related Questions