[英]When using @keyframes to fade-out with opacity, how do I repeat the animation with the event?
如果用戶嘗試拖放已經被拖動的元素,我會為用戶彈出一個標簽。
問題是,動畫只發生一次,在動畫結束時,它的不透明度永遠為 0。
CSS
@keyframes smooth {
0% { opacity: 1;}
100% { opacity: 0;}
}
.o_tip{
position: absolute;
z-index: 999;
display: none;
opacity: 0;
-webkit-animation: smooth 2s ease-in;
-moz-animation: smooth 2s ease-in;
-o-animation: smooth 2s ease-in;
-ms-animation: smooth 2s ease-in;
animation: smooth 2s ease-in;
}
為了說明我的問題,如果我在opacity: 0.2
而不是opacity: 0
上“結束”動畫:
@keyframes smooth {
0% { opacity: 1;}
100% { opacity: 0.2;}
}
...然后標簽會為每個事件重新出現 - 但它不會再次淡出,這是我想做的。
您可以將動畫規則放在特定的 css 類規則中,然后再次單擊添加該類。 請記住以下幾點:
void targetDiv.offsetWidth;
. document.querySelector("#start-animation").onclick = function(e){ var targetDiv = document.querySelector("#mydiv"); targetDiv.className = ""; void targetDiv.offsetWidth; // this triggers UI reflow targetDiv.classList.add("o_tip"); }//onclick
@keyframes smooth { 0% { opacity: 1;} 100% { opacity: 0;} } .o_tip{ z-index: 999; animation: smooth 2s ease-in forwards; } #mydiv{ background-color: yellow; height: 50px; width: 100px; } #mydiv.o_top{ display: block; }
<div id="mydiv"></div> <button id="start-animation">Start animation</button>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.