页面加载时自动播放动画(直接写 class)只覆盖了一部分场景。更多时候,动画需要由用户操作触发——点击按钮弹出弹窗、提交表单显示成功提示、切换 Tab 高亮新内容。这些都需要 JavaScript 在合适的时机添加或移除 Animate.css 的类名。
核心思路很简单:JS 负责时机判断和类名切换,CSS 负责动画实现。
var box = document.getElementById('box'); // 触发淡入动画 box.classList.add('animate__animated', 'animate__fadeInUp'); // 5秒后触发淡出 setTimeout(function() { box.classList.remove('animate__fadeInUp'); box.classList.add('animate__fadeOutUp'); }, 5000);
classList.add() 可以一次添加多个类。注意:如果要切换动画方向(从进入变为退出),需要先移除旧的动画类,再添加新的。
CSS Animation 的一个限制是:如果元素已经处于动画结束状态,再次添加相同的类名不会重新触发动画。浏览器会认为"类名没变,什么都不做"。
解决方案有两种:
方案一:强制重排
function replayAnimation(element) { element.classList.remove('animate__animated', 'animate__fadeIn'); // void 触发读取 element 的布局信息,强制浏览器重排 void element.offsetWidth; element.classList.add('animate__animated', 'animate__fadeIn'); }
void element.offsetWidth 是惯用写法——读取 offsetWidth 会迫使浏览器同步计算布局(重排),从而清除之前的动画状态。之后重新添加类名,动画会从头播放。
方案二:animation 属性重置
function replayAnimation(element) { element.style.animation = 'none'; void element.offsetWidth; element.style.animation = ''; }
直接操作 style.animation 属性。设为 none 清除动画,重排后恢复空值让 CSS 类重新生效。
方案一更符合"声明式"理念——通过类名控制动画。方案二更直接但不够优雅。实际项目中两种都可以用,选你最习惯的。

<style> .toast-container { position: fixed; top: 20px; right: 20px; z-index: 1000; } .toast { padding: 12px 24px; background: #333; color: white; border-radius: 8px; margin-bottom: 8px; opacity: 0; /* 初始不可见 */ } </style> <div class="toast-container" id="toastContainer"></div> <button onclick="showToast('操作成功')">点击提示</button> <script> function showToast(message) { var container = document.getElementById('toastContainer'); var toast = document.createElement('div'); toast.className = 'toast animate__animated animate__fadeInRight'; toast.textContent = message; container.appendChild(toast); // 3秒后淡出并移除 setTimeout(function() { toast.classList.remove('animate__fadeInRight'); toast.classList.add('animate__fadeOutRight'); toast.addEventListener('animationend', function() { toast.remove(); }); }, 3000); } </script>
要点分析:
div 时直接带上 animate__fadeInRight 类,从右侧滑入opacity: 0 防止创建时闪烁fadeOutRight,动画结束后 remove() 移除 DOM 节点animationend 事件确保动画播完才移除——如果动画时长改变,不用改 timeout 值function openModal(modalId) { var modal = document.getElementById(modalId); modal.style.display = 'flex'; // 显示 modal.classList.add('animate__animated', 'animate__zoomIn'); } function closeModal(modalId) { var modal = document.getElementById(modalId); // 先播放退出动画 modal.classList.remove('animate__zoomIn'); modal.classList.add('animate__zoomOut'); // 动画结束后隐藏 modal.addEventListener('animationend', function handler() { modal.style.display = 'none'; modal.classList.remove('animate__zoomOut', 'animate__animated'); modal.removeEventListener('animationend', handler); }); }
注意 removeEventListener 的使用——animationend 回调中需要移除自身监听器,否则会内存泄漏。
当需要同时控制一组元素的动画时,用 querySelectorAll + forEach:
// 让所有 .item 元素依次播放动画 var items = document.querySelectorAll('.item'); items.forEach(function(item, index) { item.style.setProperty('--animate-delay', (index * 0.15) + 's'); item.classList.add('animate__animated', 'animate__fadeInUp'); });
利用 CSS 变量 --animate-delay 为每个元素设置递增延迟。比用 setTimeout 逐个添加类名更优雅——所有元素在同一帧添加类名,由 CSS 的 animation-delay 控制出场顺序,避免了 JS 定时器的累积误差。
通过 getComputedStyle 获取元素当前的动画状态:
function isAnimating(element) { var style = window.getComputedStyle(element); return style.animationName !== 'none' && style.animationPlayState !== 'paused'; }
也可以通过 animationend 事件来判断动画是否完成:
element.addEventListener('animationend', function() { console.log('动画播放完毕'); });
animationend 的 event 对象包含有用信息:
element.addEventListener('animationend', function(e) { console.log('动画名称:', e.animationName); // "fadeInUp" console.log('动画时长:', e.elapsedTime); // 1(秒) console.log('伪元素:', e.pseudoElement); // "" 或 "::before" });
CSS Animation 原生支持暂停和恢复:
var element = document.querySelector('.animating'); // 暂停 element.style.animationPlayState = 'paused'; // 恢复 element.style.animationPlayState = 'running';
这比重新播放动画更轻量——不涉及重排,只是告诉浏览器"暂停在这个帧"。适合用户点击暂停、Tab 切到后台暂停动画等场景。
在 Animate.css CSS 文件加载完成之前,带动画类的元素会以默认状态显示一瞬。解决方案:
<head> <style> /* 内联关键 CSS:初始隐藏所有动画元素 */ .animate__animated { visibility: hidden; } </style> <!-- 预加载 Animate.css --> <link rel="preload" href="animate.min.css" as="style" onload="this.onload=null;this.rel='stylesheet'"> <noscript><link rel="stylesheet" href="animate.min.css"></noscript> </head>
visibility: hidden 而不是 display: none——visibility 不影响布局,切换时不会引起重排。Animate.css 加载后,animate__animated 类会设置正确的动画属性,覆盖 visibility: hidden。
如果你的退出动画(如 fadeOutDown)结束后元素消失了,但你想保留 DOM 节点用于后续操作,注意 animation-fill-mode:
/* 默认 both:动画结束后保持 to 帧状态 */ /* to 帧:opacity: 0 → 元素不可见但仍在 DOM 中 */ .animate__animated { animation-fill-mode: both; }
元素在视觉上消失了(opacity: 0),但 DOM 节点仍然存在。如果需要完全移除:
element.addEventListener('animationend', function() { element.remove(); });
框架的虚拟 DOM 更新可能与动画时序冲突。比如在 Vue 中,v-if 切换会直接销毁 DOM,导致退出动画来不及播放。这个问题的解决方案在框架集成的章节详细讨论。核心思路是:用 v-show 代替 v-if(Vue),或用 exit prop(React Transition Group),让元素在动画期间保持挂载。