У меня есть всплывающее окно, которое открывается при нажатии кнопки, а затем закрывается, если вы нажимаете на другую кнопку или за ее пределами.Я хочу, чтобы всплывающее окно исчезало при открытии и исчезало при закрытии.Как я могу переключаться между двумя ключевыми кадрами с помощью javascript?
Я пытался сделать это с переключением классов с помощью javascript, но это не работает.
var popup = document.getElementById("popup");
var popup_content = document.getElementById("popup_content");
var add = document.getElementById("add");
var span = document.getElementById("close");
add.onclick = function() {
popup.style.display = "block";
popup.className = "opened";
popup_content = "opened";
}
span.onclick = function() {
popup.style.display = "none";
popup.className = "closed";
popup_content = "closed";
}
window.onclick = function(event) {
if (event.target == popup) {
popup.style.display = "none";
popup.className = "closed";
popup_content = "closed";
}
}
#popup {
display: none;
position: fixed;
z-index: 1;
padding-top: 100px;
left: 0;
top: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.4);
}
#popup_content {
position: relative;
margin: auto;
background-color: white;
width: 80%;
max-width: 500px;
border-radius: 5px;
padding: 20px;
text-decoration: none;
box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.5);
}
.closed {
-webkit-animation-name: animate-in;
-webkit-animation-duration: 0.6s;
animation-name: animate-in;
animation-duration: 0.6s;
}
.opened {
-webkit-animation-name: animate-out;
-webkit-animation-duration: 0.6s;
animation-name: animate-out;
animation-duration: 0.6s;
}
@-webkit-keyframes animate-in {
from {
opacity: 0
}
to {
opacity: 1
}
}
@keyframes animate-in {
from {
opacity: 0
}
to {
opacity: 1
}
}
@-webkit-keyframes animate-out {
from {
opacity: 1
}
to {
opacity: 0
}
}
@keyframes animate-out {
from {
opacity: 1
}
to {
opacity: 0
}
}
#close {
float: right;
cursor: pointer;
margin: -15px -15px 0 0;
}
<button id="add">Open popup</button>
<div class="closed" id="popup">
<div class="closed" id="popup_content">
<i class="fas fa-times-circle" id="close">Close</i> //////content
</div>
</div>
Как лучше всего достичь моей цели?