Callback 函数在当前动画 100% 完成之后执行。
1、jQuery Callback
1.1 jQuery 动画的问题
许多 jQuery 函数涉及动画。这些函数也许会将 speed 或 duration 作为可选参数。
例子:$("p").hide("slow")
speed 或 duration 参数可以设置许多不同的值,比如 "slow", "fast", "normal" 或毫秒。
1 $("button").click(function(){ 2 $("p").hide(1000); 3 });
由于 JavaScript 语句(指令)是逐一执行的 - 按照次序,动画之后的语句可能会产生错误或页面冲突,因为动画还没有完成。
为了避免这个情况,您可以以参数的形式添加 Callback 函数。
1.2 jQuery Callback函数
当动画100%完成后,即调用Callback函数。
典型的语法:
1 $(selector).hide(speed,callback)
callback 参数是一个在 hide 操作完成后被执行的函数。
错误(没有callback):
1 $("p").hide(1000); 2 alert("The paragraph is now hidden");
举例说明:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 6 <meta http-equiv="Content-Language" content="zh-cn" /> 7 <title>My test page</title> 8 9 <!--引用jQuery库,src可以直接指向本地下载的jQery库--> 10 <!--<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">--> 11 <script src="jquery-3.3.1.js"></script> 12 <script type="text/javascript"> 13 $(document).ready(function() { 14 $("button").click(function() { 15 $("p").hide(2000); 16 alert("The paragraph is now hidden"); 17 }); 18 }); 19 20 </script> 21 </head> 22 23 <body> 24 25 <button type="button">Hide</button> 26 <p>This is a paragraph with little content.</p> 27 28 </body> 29 </html>
正确(有callback):
1 $("p").hide(1000, function() { 2 alert("The paragraph is now hidden"); 3 });
举例说明:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 6 <meta http-equiv="Content-Language" content="zh-cn" /> 7 <title>My test page</title> 8 9 <!--引用jQuery库,src可以直接指向本地下载的jQery库--> 10 <!--<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">--> 11 <script src="jquery-3.3.1.js"></script> 12 <script type="text/javascript"> 13 $(document).ready(function() { 14 $("button").click(function() { 15 $("p").hide(2000, function() { 16 alert("The paragraph is now hidden") 17 }); 18 }); 19 }); 20 21 </script> 22 </head> 23 24 <body> 25 26 <button type="button">Hide</button> 27 <p>This is a paragraph with little content.</p> 28 29 </body> 30 </html>
结论:如果您希望在一个涉及动画的函数之后来执行语句,请使用 callback 函数。
2、jQuery Chaning
通过 jQuery,您可以把动作/方法链接起来。
Chaining 允许我们在一条语句中允许多个 jQuery 方法(在相同的元素上)。
2.1 jQuery 方法链接
直到现在,我们都是一次写一条 jQuery 语句(一条接着另一条)。
不过,有一种名为链接(chaining)的技术,允许我们在相同的元素上运行多条 jQuery 命令,一条接着另一条。
提示:这样的话,浏览器就不必多次查找相同的元素。
如需链接一个动作,您只需简单地把该动作追加到之前的动作上。
举例(下面的例子把 css(), slideUp(), and slideDown() 链接在一起。"p1" 元素首先会变为红色,然后向上滑动,然后向下滑动):
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 6 <meta http-equiv="Content-Language" content="zh-cn" /> 7 <title>My test page</title> 8 9 <!--引用jQuery库,src可以直接指向本地下载的jQery库--> 10 <!--<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">--> 11 <script src="jquery-3.3.1.js"></script> 12 <script type="text/javascript"> 13 <!-- $(document).ready(function() { --> 14 <!-- $("button").click(function() { --> 15 <!-- $("p").css("color","red").slideUp(2000).slideDown(2000); --> 16 <!-- }); --> 17 <!-- }); --> 18 19 <!--或者写成如下语句,jQuery 会抛掉多余的空格,并按照一行长代码来执行上面的代码行--> 20 $(document).ready(function() { 21 $("button").click(function() { 22 $("p").css("color","red") 23 .slideUp(2000) 24 .slideDown(2000); 25 }); 26 }); 27 28 </script> 29 </head> 30 31 <body> 32 33 <p id="p1">jQuery 乐趣十足!</p> 34 <button>点击这里</button> 35 36 </body> 37 </html>
如果需要,我们也可以添加多个方法调用。
提示:当进行链接时,代码行会变得很差。不过,jQuery 在语法上不是很严格;您可以按照希望的格式来写,包含折行和缩进。