gpt4 book ai didi

javascript - 切换 CSS 关键帧

转载 作者:太空宇宙 更新时间:2023-11-04 15:50:35 25 4
gpt4 key购买 nike

我有一个弹出窗口,它在单击按钮时打开,然后在您单击另一个按钮或弹出窗口之外时关闭。我希望弹出窗口在打开时淡入并在关闭时淡出。如何使用 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>

实现我的目标的最佳方式是什么?

最佳答案

我会使用过渡 - 请参阅 css 和 js 中关于我更改内容的注释:

var popup = document.getElementById("popup");
var add = document.getElementById("add");
var span = document.getElementById("close");
add.onclick = function() {
popup.className = "opened"; // only need to transition the popup
}
span.onclick = function() {
popup.className = "closed";
}
window.onclick = function(event) {
if (event.target == popup) {
popup.className = "closed";
}
}
#popup {
position: fixed;
z-index: 1;
padding-top: 100px;
left: 0;
top: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.4);

/* add the following and remove the display:none */
transition: opacity 0.6s;
opacity: 0; /* start off closed and opacity 0 to hide */
}
#popup.opened {
opacity: 1; /* add opacity 1 so it transitions to be shown */
}

#popup.closed {
/* this stops the popup from overlaying the content when closed */
pointer-events:none;
}

#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);
}


#close {
float: right;
cursor: pointer;
margin: -15px -15px 0 0;
}
<button id="add">Open popup</button>


<div class="closed" id="popup">
<div id="popup_content">
<i class="fas fa-times-circle" id="close">Close</i> //////content
</div>
</div>

关于javascript - 切换 CSS 关键帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50715809/

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