如何使用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文档中的详细说明。
Kimi-K2.5Kimi K2.5 是一款开源的原生多模态智能体模型,它在 Kimi-K2-Base 的基础上,通过对约 15 万亿混合视觉和文本 tokens 进行持续预训练构建而成。该模型将视觉与语言理解、高级智能体能力、即时模式与思考模式,以及对话式与智能体范式无缝融合。Python00- QQwen3-Coder-Next2026年2月4日,正式发布的Qwen3-Coder-Next,一款专为编码智能体和本地开发场景设计的开源语言模型。Python00
xw-cli实现国产算力大模型零门槛部署,一键跑通 Qwen、GLM-4.7、Minimax-2.1、DeepSeek-OCR 等模型Go06
PaddleOCR-VL-1.5PaddleOCR-VL-1.5 是 PaddleOCR-VL 的新一代进阶模型,在 OmniDocBench v1.5 上实现了 94.5% 的全新 state-of-the-art 准确率。 为了严格评估模型在真实物理畸变下的鲁棒性——包括扫描伪影、倾斜、扭曲、屏幕拍摄和光照变化——我们提出了 Real5-OmniDocBench 基准测试集。实验结果表明,该增强模型在新构建的基准测试集上达到了 SOTA 性能。此外,我们通过整合印章识别和文本检测识别(text spotting)任务扩展了模型的能力,同时保持 0.9B 的超紧凑 VLM 规模,具备高效率特性。Python00
KuiklyUI基于KMP技术的高性能、全平台开发框架,具备统一代码库、极致易用性和动态灵活性。 Provide a high-performance, full-platform development framework with unified codebase, ultimate ease of use, and dynamic flexibility. 注意:本仓库为Github仓库镜像,PR或Issue请移步至Github发起,感谢支持!Kotlin07
VLOOKVLOOK™ 是优雅好用的 Typora/Markdown 主题包和增强插件。 VLOOK™ is an elegant and practical THEME PACKAGE × ENHANCEMENT PLUGIN for Typora/Markdown.Less00