几秒后弹出会自动消失

时间:2017-05-09 13:23:02

标签: javascript html css animation

我发现this关于如何在Javascript中点击弹出窗口,但是我想稍微改变它,所以当点击它时它会在几秒后自动消失......但我可以'让它工作,我试图将动画设置为

@keyframes fadeIn {
0% {opacity: 0;}
50% {opacity:1 ;}
100%{opacity:0;}
}

它消失了,但它又回来了......为什么会这样?

function myFunction() {
    var popup = document.getElementById("myPopup");
    popup.classList.toggle("show");
}
/* Popup container - can be anything you want */
.popup {
    position: relative;
    display: inline-block;
    cursor: pointer;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

/* The actual popup */
.popup .popuptext {
    visibility: hidden;
    width: 160px;
    background-color: #555;
    color: #fff;
    text-align: center;
    border-radius: 6px;
    padding: 8px 0;
    position: absolute;
    z-index: 1;
    bottom: 125%;
    left: 50%;
    margin-left: -80px;
}

/* Popup arrow */
.popup .popuptext::after {
    content: "";
    position: absolute;
    top: 100%;
    left: 50%;
    margin-left: -5px;
    border-width: 5px;
    border-style: solid;
    border-color: #555 transparent transparent transparent;
}

/* Toggle this class - hide and show the popup */
.popup .show {
    visibility: visible;
    -webkit-animation: fadeIn 3s;
    animation: fadeIn 3s;
}

/* Add animation (fade in the popup) */
@-webkit-keyframes fadeIn {
    0% {opacity: 0;}
    50% {opacity:1 ;}
	100%{opacity:0;}
}

@keyframes fadeIn {
    0% {opacity: 0;}
    50% {opacity:1 ;}
	100%{opacity:0;}
}
<h2>Popup</h2>
<div class="popup" onclick="myFunction()">Click me to toggle the popup!
  <span class="popuptext" id="myPopup">A Simple Popup!</span>
</div>

2 个答案:

答案 0 :(得分:1)

我认为您需要将.popup .show的默认状态设置为opacity: 0,因为在动画运行后它将返回到您定义的默认状态。

.popup .show {
    opacity: 0; /* add this */
    visibility: visible;
    -webkit-animation: fadeIn 3s;
    animation: fadeIn 3s;
}

function myFunction() {
    var popup = document.getElementById("myPopup");
    popup.classList.toggle("show");
}
/* Popup container - can be anything you want */
.popup {
    position: relative;
    display: inline-block;
    cursor: pointer;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

/* The actual popup */
.popup .popuptext {
    visibility: hidden;
    width: 160px;
    background-color: #555;
    color: #fff;
    text-align: center;
    border-radius: 6px;
    padding: 8px 0;
    position: absolute;
    z-index: 1;
    bottom: 125%;
    left: 50%;
    margin-left: -80px;
}

/* Popup arrow */
.popup .popuptext::after {
    content: "";
    position: absolute;
    top: 100%;
    left: 50%;
    margin-left: -5px;
    border-width: 5px;
    border-style: solid;
    border-color: #555 transparent transparent transparent;
}

/* Toggle this class - hide and show the popup */
.popup .show {
    opacity: 0;
    visibility: visible;
    -webkit-animation: fadeIn 3s;
    animation: fadeIn 3s;
}

/* Add animation (fade in the popup) */
@-webkit-keyframes fadeIn {
    0% {opacity: 0;}
    50% {opacity:1 ;}
	100%{opacity:0;}
}

@keyframes fadeIn {
    0% {opacity: 0;}
    50% {opacity:1 ;}
	100%{opacity:0;}
}
<h2>Popup</h2>
<div class="popup" onclick="myFunction()">Click me to toggle the popup!
  <span class="popuptext" id="myPopup">A Simple Popup!</span>
</div>

答案 1 :(得分:0)

你的淡入应该是:

@keyframes fadeIn {
0% {opacity: 0;}
100%{opacity: 1;}
}

通过像往常一样将其设置为100,它会执行fadeIn / fadeOut动画

编辑:对不起,我的问题出错了