eric
eric

Reputation: 320

Modal transition display issue

I have a modal, and I need to add a "fade in, fade out" effect.
I thought that by adding transition: display 2s the modal would have the said effect, but, it did nothing. Below is my code. What am I doing wrong here?

let handleModalOpen = (e) => {
  $("[data-tzilla-modal='modalxyz'].modal").css('display', 'block');
  $(document).on('click', handleModalClose);
}
let handleModalClose = (e) => {
  if (e.target === $("[data-tzilla-modal='modalxyz'].modal")[0]) {
    $("[data-tzilla-modal='modalxyz'].modal").css('display', 'none');
    $(document).off('click', handleModalClose);
  }
}
$(document).on('click', "[data-tzilla-modal='modalxyz']", handleModalOpen);
.modal {
  display: none;
  position: fixed;
  z-index: 1;
  padding-top: 50px;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  overflow: auto;
  background-color: rgb(0, 0, 0);
  background-color: rgba(0, 0, 0, 0.4);
  transition: display 2s;
}

.modal-content {
  background-color: #fefefe;
  margin: auto;
  padding: 20px;
  border: 1px solid #888;
  width: 80%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button data-tzilla-modal='modalxyz'>test modal</button>
<div id="myModal" data-tzilla-modal='modalxyz' class="modal">
  <div class="modal-content">
    <p>Some text in the Modal..</p>
  </div>
</div>

Upvotes: 2

Views: 42

Answers (2)

FluxCoder
FluxCoder

Reputation: 1276

The CSS property display doesn't allow for transitions, instead try using visibility property.

Replace

display: none;

to

visibility: hidden;

Upvotes: 0

SilentCoder
SilentCoder

Reputation: 2000

Use fadeOut( "slow" ); and fadeIn( "slow" ); insted of css()

let handleModalOpen = (e) => {
  $("[data-tzilla-modal='modalxyz'].modal").fadeIn( "slow" );
  $(document).on('click', handleModalClose);
}
let handleModalClose = (e) => {
  if (e.target === $("[data-tzilla-modal='modalxyz'].modal")[0]) {
    $("[data-tzilla-modal='modalxyz'].modal").fadeOut( "slow" );
    $(document).off('click', handleModalClose);
  }
}
$(document).on('click', "[data-tzilla-modal='modalxyz']", handleModalOpen);
.modal {
  display: none;
  position: fixed;
  z-index: 1;
  padding-top: 50px;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  overflow: auto;
  background-color: rgb(0, 0, 0);
  background-color: rgba(0, 0, 0, 0.4);
  transition: display 2s;
}

.modal-content {
  background-color: #fefefe;
  margin: auto;
  padding: 20px;
  border: 1px solid #888;
  width: 80%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button data-tzilla-modal='modalxyz'>test modal</button>
<div id="myModal" data-tzilla-modal='modalxyz' class="modal">
  <div class="modal-content">
    <p>Some text in the Modal..</p>
  </div>
</div>

Upvotes: 1

Related Questions