CSS Animation 提供三个生命周期事件:
| 事件 | 触发时机 | 浏览器支持 |
|---|---|---|
animationstart |
动画开始播放(包括 delay 结束后) | 全部 |
animationiteration |
每次重复播放开始(animation-iteration-count > 1) |
全部 |
animationend |
动画播放完成 | 全部 |
最常用的是 animationend。它在动画到达 100% 关键帧时触发,是构建动画链的核心事件。
var element = document.querySelector('.box'); element.addEventListener('animationend', function(e) { console.log('动画名称:', e.animationName); console.log('实际播放时长:', e.elapsedTime + 's'); console.log('触发元素:', e.target); });
一个元素先后播放两个动画——先淡入,停顿一会,再淡出:
var el = document.getElementById('message'); // 第一阶段:淡入 el.classList.add('animate__animated', 'animate__fadeIn'); el.addEventListener('animationend', function onFadeInEnd(e) { if (e.animationName !== 'fadeIn') return; // 只响应 fadeIn el.removeEventListener('animationend', onFadeInEnd); // 第二阶段:停顿后淡出 setTimeout(function() { el.classList.remove('animate__fadeIn'); el.classList.add('animate__fadeOut'); }, 2000); });
关键细节:if (e.animationName !== 'fadeIn') return。如果一个元素同时播放多个动画(后面会讲),animationend 会在每个动画结束时各触发一次。通过 animationName 区分是哪个动画结束了。
场景:第一步播放完 → 第二步播放 → 第三步播放。三个不同元素依次动画。
var step1 = document.getElementById('step1'); var step2 = document.getElementById('step2'); var step3 = document.getElementById('step3'); function animateSequence(steps) { var index = 0; function playNext() { if (index >= steps.length) return; var current = steps[index]; current.el.classList.add('animate__animated', current.animation); current.el.addEventListener('animationend', function handler(e) { current.el.removeEventListener('animationend', handler); index++; playNext(); }); } playNext(); } animateSequence([ { el: step1, animation: 'animate__fadeInLeft' }, { el: step2, animation: 'animate__fadeInUp' }, { el: step3, animation: 'animate__fadeInRight' } ]);
这是一个通用的串联动画执行器。steps 数组定义了动画顺序,每一步播放完毕后自动触发下一步。

用 Promise 包装 animationend,让动画链可以用 async/await 写法:
function waitForAnimation(element, animationName) { return new Promise(function(resolve) { function handler(e) { if (animationName && e.animationName !== animationName) return; element.removeEventListener('animationend', handler); resolve(e); } element.addEventListener('animationend', handler); }); } // 使用:async/await 风格 async function playSequence() { var box = document.getElementById('box'); // 第一步:弹入 box.classList.add('animate__animated', 'animate__bounceIn'); await waitForAnimation(box, 'bounceIn'); // 第二步:抖两下(吸引注意) box.classList.remove('animate__bounceIn'); box.classList.add('animate__shakeX'); await waitForAnimation(box, 'shakeX'); // 第三步:淡出 box.classList.remove('animate__shakeX'); box.classList.add('animate__fadeOut'); await waitForAnimation(box, 'fadeOut'); console.log('全部动画完成'); } playSequence();
这种写法比嵌套回调清晰得多,也更容易处理错误和中断。
动画可能因为各种原因不触发 animationend——比如元素被移除、prefers-reduced-motion 取消了动画、或 animation-duration: 0s。加一个超时保护:
function waitForAnimation(element, animationName, timeout) { timeout = timeout || 5000; return new Promise(function(resolve) { var settled = false; function done(e) { if (settled) return; settled = true; element.removeEventListener('animationend', handler); clearTimeout(timer); resolve(e); } function handler(e) { if (animationName && e.animationName !== animationName) return; done(e); } var timer = setTimeout(function() { done(null); // 超时,resolve null }, timeout); element.addEventListener('animationend', handler); }); }
// 表单第一步完成后,引导到第二步 async function multiStepForm() { var step1 = document.getElementById('form-step-1'); var step2 = document.getElementById('form-step-2'); // 验证第一步 if (!validateStep1()) { step1.classList.add('animate__animated', 'animate__shakeX'); await waitForAnimation(step1, 'shakeX'); step1.classList.remove('animate__shakeX'); return; } // 第一步退出,第二步进入 step1.classList.add('animate__fadeOutLeft'); step2.classList.add('animate__animated', 'animate__fadeInRight'); step2.style.display = 'block'; await Promise.all([ waitForAnimation(step1, 'fadeOutLeft'), waitForAnimation(step2, 'fadeInRight') ]); step1.style.display = 'none'; }
async function slideToNext(slides, currentIndex) { var currentSlide = slides[currentIndex]; var nextIndex = (currentIndex + 1) % slides.length; var nextSlide = slides[nextIndex]; // 当前幻灯片退出 currentSlide.classList.add('animate__fadeOutLeft'); // 下一张进入 nextSlide.style.display = 'block'; nextSlide.classList.add('animate__animated', 'animate__fadeInRight'); await Promise.all([ waitForAnimation(currentSlide, 'fadeOutLeft'), waitForAnimation(nextSlide, 'fadeInRight') ]); currentSlide.style.display = 'none'; currentSlide.classList.remove('animate__fadeOutLeft'); }
async function submitForm() { var btn = document.getElementById('submitBtn'); var status = document.getElementById('status'); // 按钮变为加载态 btn.textContent = '提交中...'; btn.classList.add('animate__animated', 'animate__pulse', 'animate__infinite'); try { var result = await api.submit(); // 加载结束,显示成功 btn.classList.remove('animate__pulse', 'animate__infinite'); btn.classList.add('animate__bounce'); status.textContent = '提交成功!'; status.classList.add('animate__animated', 'animate__fadeInUp'); await waitForAnimation(btn, 'bounce'); btn.classList.remove('animate__bounce'); } catch (err) { btn.classList.remove('animate__pulse', 'animate__infinite'); btn.classList.add('animate__shakeX'); await waitForAnimation(btn, 'shakeX'); btn.classList.remove('animate__shakeX'); } }
伪元素动画。如果动画作用在 ::before 或 ::after 伪元素上,animationend 的 e.pseudoElement 会是 "::before" 或 "::after"。
多次重复。如果 animation-iteration-count: 3,animationend 只在最后一次播放结束时触发。中间的重复会触发 animationiteration。
取消动画。如果在动画播放过程中移除了动画类,animationend 不会触发。如果你需要在动画被取消时也执行清理逻辑,可以用 animationcancel 事件(Firefox 支持,Chrome 84+ 支持)。
element.addEventListener('animationcancel', function() { console.log('动画被取消'); });
动画延迟期间取消。如果动画在延迟期间被取消(比如元素被 display: none),浏览器行为不完全一致。建议在延迟开始前设置 animation-fill-mode: backwards(animate__animated 的 both 已包含),这样取消时元素会回到初始状态。