简体   繁体   English

CSS Transition没有jQuery; 显示然后隐藏

[英]CSS Transition No jQuery; Display then Hide

I built a card game where upon clicking a button the display will either be "Correct!" 我建立了一个纸牌游戏,点击按钮后显示的画面将为“正确!”。 or "Wrong!" 或“错!” I would like the display to flash and then go away after a couple second but not rearrange the content below it, which in this case is the #winStreak and #longestStreak. 我希望显示屏闪烁,然后在几秒钟后消失,但不要重新排列其下方的内容,在本例中为#winStreak和#longestStreak。 I do not want to use jQuery. 我不想使用jQuery。 I have tried adding transitions to CSS, but that does not seem to work. 我尝试将过渡添加到CSS,但这似乎不起作用。

HTML: HTML:

<p id="displayResult"></p>
<p id="winStreak"></p>
<p id="longestStreak"></p>

CSS: CSS:

#displayResult {
  margin-bottom: 1rem;
  transition: 2s ease-in-out;
}

JavaScript: JavaScript:

let foldButton = document.getElementById("foldBTN")
foldButton.addEventListener("click", function(){
  if (!table[position].includes(completeHand) && !table[position].includes(completeHand2)) {
    document.getElementById("displayResult").innerHTML = "Correct!";

I believe you might use CSS animation : 我相信您可能会使用CSS animation

 var result = document.getElementById('displayResult'); result.addEventListener('animationend', function() { result.classList.remove('flashit'); }); let foldButton = document.getElementById('foldBTN'); foldButton.addEventListener('click', function() { result.innerHTML = 'Correct!'; result.classList.add('flashit'); }); 
 #displayResult { font-size:3rem; height: 1.3em; margin-bottom: 1rem; opacity: 0; } #displayResult.flashit { animation: flashit 3s; } @keyframes flashit { 5% {opacity:.5} 10% {opacity:1} 15% {opacity:.2} 20% {opacity:1} 40% {opacity:1} 100% {opacity:0} } 
 <div id="displayResult"></div> <button id="foldBTN">CLICK ME</button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM