如何使用You-Dont-Need-jQuery实现流畅的动画队列:3种高效顺序执行技巧
You-Dont-Need-jQuery是一个展示如何用原生JavaScript替代jQuery功能的优质开源项目,它提供了丰富的DOM操作、事件处理和动画实现方案。本文将聚焦于如何在不依赖jQuery的情况下,通过原生API实现动画队列的顺序执行,让你的网页动画更加流畅可控。
为什么选择原生JavaScript动画队列?
传统的jQuery动画队列虽然方便,但会增加项目体积且依赖整个库。随着现代浏览器对原生API的完善支持,使用requestAnimationFrame和Promise等技术实现动画队列,不仅能减少资源加载,还能获得更好的性能和控制精度。
原生动画的核心优势
- 轻量级:无需加载额外库,减少HTTP请求
- 高性能:利用浏览器原生优化,减少重绘重排
- 灵活可控:精确控制动画时序和执行流程
- 兼容性好:支持IE10+及所有现代浏览器
实现顺序动画的3种核心方法
1. 回调函数嵌套法(基础方案)
通过在每个动画的transitionend事件中触发下一个动画,形成链式调用。这种方法简单直观,兼容性好。
// 获取DOM元素
const box = document.querySelector('.animate-box');
// 定义动画步骤
function animateStep1() {
box.style.transition = 'transform 0.5s ease';
box.style.transform = 'translateX(100px)';
box.addEventListener('transitionend', animateStep2);
}
function animateStep2() {
box.removeEventListener('transitionend', animateStep2);
box.style.transition = 'opacity 0.5s ease';
box.style.opacity = '0.5';
box.addEventListener('transitionend', animateStep3);
}
function animateStep3() {
box.removeEventListener('transitionend', animateStep3);
box.style.transition = 'height 0.5s ease';
box.style.height = '200px';
}
// 启动动画队列
animateStep1();
2. Promise链式调用(现代方案)
将每个动画步骤封装为Promise,通过.then()方法实现顺序执行。这种方法代码结构清晰,便于维护和扩展。
// 封装动画为Promise
function animate(element, styles, duration) {
return new Promise(resolve => {
// 设置过渡属性
element.style.transition = `all ${duration}ms ease`;
// 应用样式
Object.keys(styles).forEach(key => {
element.style[key] = styles[key];
});
// 监听动画结束
const handleTransitionEnd = () => {
element.removeEventListener('transitionend', handleTransitionEnd);
resolve();
};
element.addEventListener('transitionend', handleTransitionEnd);
});
}
// 使用Promise链式调用执行动画队列
const box = document.querySelector('.animate-box');
animate(box, { transform: 'translateX(100px)' }, 500)
.then(() => animate(box, { opacity: '0.5' }, 500))
.then(() => animate(box, { height: '200px' }, 500))
.then(() => console.log('动画队列执行完成'));
3. async/await语法糖(优雅方案)
结合ES7的async/await语法,将异步动画代码写成同步形式,进一步提升可读性和可维护性。
// 使用async/await执行动画队列
async function runAnimationQueue() {
const box = document.querySelector('.animate-box');
await animate(box, { transform: 'translateX(100px)' }, 500);
await animate(box, { opacity: '0.5' }, 500);
await animate(box, { height: '200px' }, 500);
console.log('动画队列执行完成');
}
// 启动动画
runAnimationQueue();
动画队列高级应用技巧
动态控制动画流程
通过条件判断和中断机制,可以实现复杂的动画逻辑控制,例如根据用户交互动态调整动画顺序。
async function conditionalAnimation() {
const box = document.querySelector('.animate-box');
const userPreference = localStorage.getItem('animationPreference');
await animate(box, { transform: 'translateX(100px)' }, 500);
// 根据条件决定下一步动画
if (userPreference === 'fast') {
await animate(box, { opacity: '0.5' }, 300);
} else {
await animate(box, { opacity: '0.5' }, 800);
}
// 可取消的动画步骤
const abortController = new AbortController();
const animation = animateWithAbort(box, { height: '200px' }, 500, abortController.signal);
// 500ms后取消动画(示例)
setTimeout(() => abortController.abort(), 500);
try {
await animation;
} catch (e) {
if (e.name === 'AbortError') {
console.log('动画已取消');
}
}
}
并行与顺序混合动画
结合Promise.all()可以实现部分动画并行执行,优化整体动画时间。
async function mixedAnimation() {
const box1 = document.querySelector('.box-1');
const box2 = document.querySelector('.box-2');
// 并行执行两个动画
await Promise.all([
animate(box1, { transform: 'translateX(100px)' }, 500),
animate(box2, { transform: 'translateX(-100px)' }, 500)
]);
// 顺序执行后续动画
await animate(box1, { opacity: '0.5' }, 300);
await animate(box2, { opacity: '0.5' }, 300);
}
浏览器兼容性与性能优化
兼容处理
虽然现代浏览器已广泛支持transition和Promise,但为确保最佳兼容性,可添加简单的特性检测:
// 特性检测示例
function supportsTransitions() {
const style = document.createElement('div').style;
return 'transition' in style ||
'WebkitTransition' in style ||
'MozTransition' in style ||
'msTransition' in style;
}
// 降级处理
if (!supportsTransitions()) {
console.warn('浏览器不支持CSS过渡动画,将使用降级方案');
// 实现简单的定时器动画作为降级方案
}
性能优化建议
-
使用
will-change提示浏览器优化.animate-box { will-change: transform, opacity; } -
避免同时触发过多动画
- 控制同一时间执行的动画数量
- 使用
requestAnimationFrame确保动画同步浏览器重绘
-
优化动画属性
- 优先使用
transform和opacity属性,它们不会触发重排 - 避免动画
width、height等会导致重排的属性
- 优先使用
总结
通过You-Dont-Need-jQuery项目倡导的原生API方案,我们可以抛弃jQuery的动画队列,用更现代、更高效的方式实现动画顺序执行。无论是基础的回调嵌套、优雅的Promise链式调用,还是简洁的async/await语法,都能帮助我们构建流畅、可控的动画效果。
掌握这些技巧后,你可以在项目中实现复杂的动画序列,同时保持代码的轻量和高性能。开始尝试用原生JavaScript打造属于你的动画队列吧!
要开始使用这个项目,你可以通过以下命令克隆仓库:
git clone https://gitcode.com/gh_mirrors/you/You-Dont-Need-jQuery
项目中更多关于动画和DOM操作的原生实现,可以参考README.zh-CN.md文档中的详细说明。
atomcodeClaude Code 的开源替代方案。连接任意大模型,编辑代码,运行命令,自动验证 — 全自动执行。用 Rust 构建,极致性能。 | An open-source alternative to Claude Code. Connect any LLM, edit code, run commands, and verify changes — autonomously. Built in Rust for speed. Get StartedRust0201
cann-learning-hubCANN 学习中心仓,支持在线互动运行、边学边练,提供教程、示例与优化方案,一站式助力昇腾开发者快速上手。Jupyter Notebook0130
MiMo-V2.5-Pro-FP4-DFlashMiMo-V2.5-Pro-FP4-DFlash 是驱动 MiMo-V2.5-Pro-UltraSpeed 的底层模型: FP4 量化骨干网络:对 MoE 专家采用 MXFP4 量化,同时保持模型其他部分的更高精度,在几乎无损质量的前提下,显著减小模型体积并降低内存带宽压力。 BF16 DFlash 草稿生成器:用于块扩散推测解码,每次前向传播可生成一整个块的 tokens,并让骨干网络一步完成验证。 两者协同作用,既降低了每参数的位宽,又减少了骨干网络前向传播的次数,而这两者正是万亿参数模型解码过程中的两大主要成本来源。Python00
JoyAI-EchoJoyAI-Echo,这是一个独立的、仅用于推理的版本,旨在实现分钟级多镜头音视频生成。它采用了经过蒸馏的DMD生成器、配对的跨模态记忆以及故事级别的一致性。其性能的核心在于,一个跨模态视听记忆库能够在长达五分钟的视频中保持角色外观和语音音色的一致性。同时,一个训练后处理流程将基于记忆的强化学习与分布匹配蒸馏相结合,实现了7.5倍的速度提升,显著增强了视觉质量和对齐效果。00
AstrBot✨ 易上手的多平台 LLM 聊天机器人及开发框架 ✨ 平台支持 QQ、QQ频道、Telegram、微信、企微、飞书 | OpenAI、DeepSeek、Gemini、硅基流动、月之暗面、Ollama、OneAPI、Dify 等。附带 WebUI。Python08
handy-ollama动手学Ollama,CPU玩转大模型部署,在线阅读地址:https://datawhalechina.github.io/handy-ollama/Jupyter Notebook07