深入学习jQuery动画队列

前面的话
队列实现是jQuery非常棒的一个拓展,使用动画队列可以使动画更容易实现。本文将详细介绍jQuery动画队列
queue()
queue()方法用来显示在匹配的元素上的已经执行的函数队列
queue([queueName])
queue()方法可以接受一个可选参数——一个含有队列名的字符串。该参数默认是'fx'
<script src="http://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script> <button id="btn">开始动画</button> <button id="reset">恢复</button> <span id="result"></span> <div id="box" style="position:relative;height: 100px;width: 300px;background-color: lightblue"></div> <script> $('#reset').click(function(){ history.go(); }) $('#btn').click(function(event){ setInterval(function(){ $('#result').html('队列数是:' +$('#box').queue().length) },100) $('#box').animate({'left':'100px'},1000).animate({'width':'200px'},1000).animate({'left':'0'},1000).animate({'width':'100px'},1000); }); </script>
queue(callback(next))
queue()方法可以接受一个回调函数作为参数,表示将要添加到队列中的新函数
[注意]queue()方法的回调函数中,可以进行样式变换等,但不可以增加动画效果
由下面代码执行结果可以看出,队列执行完函数后,队列后面的动画效果被停止,这时就需要用到下面要介绍的dequeue()方法
<script src="http://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script> <button id="btn">开始动画</button> <button id="reset">恢复</button> <span id="result"></span> <div id="box" style="position:relative;height: 100px;width: 300px;background-color: lightblue"></div> <script> $('#reset').click(function(){ history.go(); }) $('#btn').click(function(event){ setInterval(function(){ $('#result').html('队列数是:' +$('#box').queue().length) },100) $('#box').animate({'left':'100px'},1000).animate({'width':'200px'},1000); $('#box').queue(function(){ $('#box').css('background','lightgreen'); }) $('#box').animate({'left':'0'},1000).animate({'width':'100px'},1000); }); </script>
dequeue()
dequeue()方法用来执行匹配元素队列的下一个函数
dequeue([queueName])
dequeue()方法可以接受一个可选参数——一个含有队列名的字符串,默认是fx
[注意]dequeue()方法本身也算队列的一员
<script src="http://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script> <button id="btn">开始动画</button> <button id="reset">恢复</button> <span id="result"></span> <div id="box" style="position:relative;height: 100px;width: 300px;background-color: lightblue"></div> <script> $('#reset').click(function(){ history.go(); }) $('#btn').click(function(event){ setInterval(function(){ $('#result').html('队列数是:' +$('#box').queue().length) },100) $('#box').animate({'left':'100px'},1000).animate({'width':'200px'},1000); $('#box').queue(function(){ $(this).css('background','lightgreen'); $(this).dequeue(); }) $('#box').animate({'left':'0'},1000).animate({'width':'100px'},1000); }); </script>
clearQueue()
与deQueue()方法相反,clearQueue()方法用来从列队中移除所有未执行的项
[注意]clearQueue()并不影响当前动画效果
clearQueue([queueName])
clearQueue()方法可以接受一个可选参数——一个含有队列名的字符串,默认是fx
<script src="http://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script> <button id="btn">开始动画</button> <button id="btn1">停止动画</button> <button id="reset">恢复</button> <span id="result"></span> <div id="box" style="position:relative;height: 100px;width: 300px;background-color: lightblue"></div> <script> $('#reset').click(function(){ history.go(); }) $('#btn').click(function(event){ setInterval(function(){ $('#result').html('队列数是:' +$('#box').queue().length) },100) $('#box').animate({'left':'100px'},1000).animate({'width':'200px'},1000); $('#box').queue(function(){ $(this).css('background','lightgreen'); $(this).dequeue(); }) $('#box').animate({'left':'0'},1000).animate({'width':'100px'},1000); }); $('#btn1').click(function(event){ $('#box').clearQueue(); }) </script>
好的代码像粥一样,都是用时间熬出来的

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构