gpt4 book ai didi

javascript - 模态转换显示问题

转载 作者:行者123 更新时间:2023-11-28 00:42:19 24 4
gpt4 key购买 nike

我有一个模态,我需要添加一个“淡入淡出”效果。
我认为通过添加 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>

最佳答案

使用 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>

关于javascript - 模态转换显示问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52712641/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com