模态转换显示问题

时间:2018-10-09 02:47:27

标签: javascript jquery html css modal-dialog

我有一个模态,我需要添加一个“淡入淡出”效果。
我以为通过添加transition: display 2s模态将具有上述效果,但是它什么也没做。下面是我的代码。我这是怎么了?

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>

2 个答案:

答案 0 :(得分:1)

使用由fadeOut( "slow" );插入的fadeIn( "slow" );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>

答案 1 :(得分:0)

CSS属性display不允许进行过渡,请尝试使用visibility属性。

替换

display: none;

visibility: hidden;