我有一个弹出按钮点击按钮然后关闭,如果你点击另一个按钮或弹出窗口外。我希望弹出窗口在打开时淡入,在关闭时淡出淡出。如何使用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>
实现目标的最佳途径是什么?
答案 0 :(得分:1)
使用CSS应用动画。不要使用display none / block属性或snippet。您可以使用过渡属性和不透明度属性来淡入/淡出。
以下是代码
的示例
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.className="opened";
popup_content.className="opened";
}
span.onclick = function() {
popup.className="closed";
popup_content.className="closed";
}
window.onclick = function(event) {
if (event.target == popup) {
popup.className="closed";
popup_content.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);
}
#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 {
opacity:0;
visibility:hidden;
transition: opacity 0.8s ease;
}
.opened {
opacity:1;
visibility:visible;
transition: opacity 0.8s ease;
}
#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>
答案 1 :(得分:1)
我会使用转换 - 请参阅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";
}
}
&#13;
#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;
}
&#13;
<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>
&#13;
答案 2 :(得分:1)
Mahmoud在尝试引入转换或动画时使用display:none / block是正确的。这里的过渡对动画也是最好的。我没有在这里包含使用classList.add / remove会让你在以后使用类时更灵活,因为它不是问题,但我认为这是重要的侧面说明。这里的不同之处在于将visibility属性包含在打开/关闭类的转换中。由于可见性是二进制的,就像使用display:none / block一样,这将删除从站点的转换。例如,关闭弹出窗口时,可见性:隐藏;属性将在转换执行开始时执行。结果,您在关闭时永远不会看到转换。如果您在转换过程中包含可见性,则会在转换的适当时间执行。我在firefox和chrome上测试了这个。
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.className="opened";
popup_content.className="opened";
}
span.onclick = function() {
popup.className="closed";
popup_content.className="closed";
}
window.onclick = function(event) {
if (event.target == popup) {
popup.className="closed";
popup_content.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);
}
#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 {
opacity:0;
visibility:hidden;
transition: opacity 0.8s ease, visibility 0.8s ease;
}
.opened {
opacity:1;
visibility:visible;
transition: opacity 0.8s ease, visibility 0.8s ease;
}
#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>