【发布时间】:2021-10-15 21:20:29
【问题描述】:
单击按钮时,我正在努力使模态变为动画。模态框在隐藏时不得占用空间。所以我想在未显示时使用 display none 。当单击按钮时,模式需要淡入。现在我找到了一个我认为可以让它工作的解决方案。但它仍然无法正常工作。谁能告诉我为什么它不起作用?我怎样才能让它工作?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Document</title>
</head>
<body>
<h2>Animating from <code>display:none</code> to <code>display:block</code></h2>
<div id="box" class="box"></div>
<button>TOGGLE VISIBILITY</button>
</body>
<script src="script.js"></script>
</html>
.box {
background: goldenrod;
width: 300px;
height: 300px;
margin: 30px auto;
transition: all 2s linear;
display: none;
}
.show {
display: block;
}
.visuallyshow {
opacity: 0;
}
button {
display: block;
margin: 0 auto;
}
let box = document.getElementById('box'),
btn = document.querySelector('button');
btn.addEventListener('click', function () {
if (box.classList.contains('show')) {
box.classList.remove('show');
setTimeout(function () {
box.classList.remove('visuallyshow');
}, 20);
} else {
box.classList.add('visuallyshow');
box.addEventListener('transitionend', function (e) {
box.classList.add('show');
}
// , {
// capture: false,
// once: true,
// passive: false
// }
);
}
}, false);
【问题讨论】:
标签: javascript css animation modal-dialog fadein