如何在播放后重置 CSS 过渡

How to reset CSS transition once it played

如果您按下 space 按钮,我会出现一个加号。但是现在它出现了一次。你能帮我让它在我每次按下 space 按钮时出现吗?这是我的 Code Pen.

import './style.scss';

let counter = 0;

document.addEventListener('keydown', ({ keyCode }) => {
  const increment = document.getElementsByClassName('increment')[0];
  if (keyCode === 32) {
    counter++;
    document.getElementsByClassName('counter')[0].innerText = counter;

    increment.classList.remove('hidden');
    increment.classList.add('move-increment');
  }
});
.container {
   /* ... */

  .counter {
    background-color: gray;
    color: white;
    border-radius: 10px;
    padding: 10px;
  }

  .move-increment {
    margin-top: -20px;
    opacity: 0;
  }

  .hidden {
    visibility: hidden;
  }

  .increment {
    position: absolute;
    margin-left: -33px;
    z-index: 1;
    transition: margin-top 1s cubic-bezier(0, 0.5, 0.5, 1),
      opacity 1s ease-in-out;
  }
}

删除添加的 .move-increment 并再次添加删除的 hidden 类 稍有延迟,这将重新应用您的 transition: margin-top(在提供的链接中阅读延迟原因):

    setTimeout(function() {increment.classList.add('hidden');
    increment.classList.remove('move-increment');}, 600);   

解决方案(将键码更改为向上箭头:↑):

let counter = 0;

document.addEventListener('keydown', ({
    keyCode
  }) =>

  {
    const increment = document.getElementsByClassName('increment')[0];
    if (keyCode === 38) {
      counter++;
      document.getElementsByClassName('counter')[0].innerText = counter;

      increment.classList.remove('hidden');
      increment.classList.add('move-increment');

      setTimeout(function() {
        increment.classList.add('hidden');
        increment.classList.remove('move-increment');
      }, 600);
    }
  });
.container {
  font-family: Arial, Helvetica, sans-serif;
  font-size: 40px;
  font-weight: bold;
  display: flex;
  height: 100px;
  align-items: center;
  justify-content: center;
}

.counter {
  background-color: gray;
  color: white;
  border-radius: 10px;
  padding: 10px;
}

.move-increment {
  margin-top: -20px;
  opacity: 0;
}

.hidden {
  visibility: hidden;
}

.increment {
  position: absolute;
  margin-left: -33px;
  z-index: 1;
  transition: margin-top 1s cubic-bezier(0, 0.5, 0.5, 1), opacity 1s ease-in-out;
}
<div class="container">
  <div>
    Counter: <span class="counter">0</span>
    <span class="increment hidden">+</span>
  </div>
</div>

但是,当按键太快时,这并不能很好地工作。尝试更改 setTimeout 持续时间,看看适合您的需要。

在提供的链接中,您有示例如何一起重置动画(而不是过渡)并解决此快速按键问题。

在此处阅读相关内容,很少有真正有用的信息:

Restart CSS Animation

Controlling CSS Animations and Transitions with JavaScript

虽然我认为@ikiK 的回答是正确的答案,因为这个问题专门关于使用 CSS 转换,但我想分享一种不同的方法。我认为 'plus' 图标的目标是在每次计数器递增时显示。但是当计数器增加而前一个增量的过渡仍在播放时,不可能显示第二个 'plus' 符号。

我的建议是使用一些 jQuery,并且在每次递增时,将一个新的 li 项目附加到位于计数器顶部的无序列表中。为 li 设置动画,使其淡出到顶部。然后使用 animate() 的回调函数从 DOM 中删除 li 元素,一旦它淡出视图。

let counter = 1;
$(document).on( 'keypress',function(e) {
    if( e.which == 32 ) {
        $('.counter').text(counter++);
        let increment = $('<li><span class="increment">+</span></li>');
        $('#increments').append(increment);
        increment.animate({
            opacity: 0,
            top: '-=30px'
        }, 500, function() {
            increment.remove();
        });
    }
});
.container {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 40px;
    font-weight: bold;
    display: flex;
    height: 500px;
    align-items: top;
    justify-content: center;
    position: relative;
  overflow: hidden;
  height: 100px;
}

.counter {
    background-color: gray;
    color: white;
    border-radius: 10px;
    padding: 10px;
}

#increments {
    padding: 0px;
    z-index: 1;
    float: left;
    margin-left: -33px;
    list-style: none;
}

#increments li {
    position: absolute;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
    <p>Counter: <span class="counter">0</span></p>
    <ul id="increments"></ul>
</div>

我认为你不需要隐藏class,只需使用setTimeout 重置class,像这样:

let counter = 0;

document.addEventListener("keydown", ({ keyCode }) => {
  const increment = document.getElementsByClassName("increment")[0];
  if (keyCode === 32) {
    counter++;
    document.getElementsByClassName("counter")[0].innerText = counter;
    increment.classList.add("move-increment");
    setTimeout(function () {
      increment.classList.remove("move-increment");
    }, 1000);
  }
});
.container {
     font-family: Arial, Helvetica, sans-serif;
     font-size: 40px;
     font-weight: bold;
     display: flex;
     height: 500px;
     align-items: center;
     justify-content: center;
}
 .container .counter {
     background-color: gray;
     color: white;
     border-radius: 10px;
     padding: 10px;
}
 .container .increment {
     position: absolute;
     margin-left: -33px;
     z-index: 1;
     visibility: hidden;
     margin-top: 0;
     opacity: 1;
     transition: margin-top 1s cubic-bezier(0, 0.5, 0.5, 1), opacity 1s ease-in-out;
}
 .container .increment.move-increment {
     visibility: visible;
     margin-top: -20px;
     opacity: 0;
}
 
 <div class="container">
   <div>
     Counter: <span class="counter">0</span>
     <span class="increment">+</span>
   </div>
 </div>