.hide {
transition: all 1s;
-webkit-transition: all 1s;
display: none;
}
#bg-mask {
transition: all 1s;
-webkit-transition: all 1s;
backdrop-filter: blur(6px);
position: fixed;
left: 0;
top: 0;
bottom: 0;
right: 0;
background-color: black;
opacity: 0.5;
}
$(function () {
$("#couponText").click(function (e) {
$('#popup').removeClass('hide')
$("#bg-mask").removeClass("hide")
e.stopPropagation()
})
$("#dialogClose").click(function () {
$('#popup').addClass('hide')
})
document.body.onclick = function closePopup(e) {
if (!$("#popup").hasClass("hide") && !$(e.target).closest($('#popup')).length) {
$("#popup").addClass("hide")
$("#bg-mask").addClass("hide")
$(document).unbind('click', closePopup);
}
}
})
因为
.hide
把元素直接display:none
了,就不会触发opacity
属性的渐变了。你得把
display:none
修改成opacity:0
才行。最后动画执行完毕再去把
#bg-mask
设置为display:none
。中间可能会造成背景元素无法点击,所以可以同时再
.hide
类上添加pointer-events: none
属性使点击效果穿透。说个题外话,既然你已经使用了
jQuery
那么可以使用jQuery
提供的.fadeIn()
和.fadeOut()
两个方法来做渐入显隐。