css动画已经执行过一次如何再次执行?

.shakeAnimate {
    animation: shakeAnimation 2s ease-in-out forwards;
  }
const showAnimation = () => {
  const box = document.querySelector('.box')
  if (box) {
    if (box.classList.contains('shakeAnimate')) {
      box.classList.remove('shakeAnimate')
      // 强制重绘
      box.offsetWidth // eslint-disable-line no-unused-expressions
    }
    box.classList.add('shakeAnimate')
  }
}

在需要展示动画的地方调用此方法即可。

posted @ 2025-10-10 16:48  蓓蕾心晴  阅读(7)  评论(0)    收藏  举报