jQuery animate() 方法用于创建自定义动画。
语法:$(selector).animate({params},speed,callback);
必需的 params 参数定义形成动画的 CSS 属性。
可选的 speed 参数规定效果的时长。它可以取以下值:"slow"、"fast" 或毫秒。
可选的 callback 参数是动画完成后所执行的函数名称。
下面的例子演示 animate() 方法的简单应用;它把元素移动到左边,3秒时间到 left 属性等于 500 像素为止:
- 1 <!DOCTYPE html>
- 2 <html lang="en">
- 3 <head>
- 4 <meta charset="UTF-8">
- 5 <title>动画</title>
- 6 <style>
- 7 *{margin:0;padding:0;}
- 8 #div2{width:100px;height:100px;background:orange;position:absolute;}
- 9 </style>
- 10 <!-- jquery自己引入就好,我这里的是jquery-1.12.0.min.js -->
- 11 <script type="text/javascript" src="jquery-1.12.0.min.js"></script>
- 12 <script type="text/javascript">
- 13 $(document).ready(function(){
- 14 $("#bt1").click(function(){
- 15 $("#div2").animate({
- 16 left:'500px',
- 17 top:'150px',
- 18 opacity:'0.5',
- 19 },3000);
- 20 });
- 21 });
- 22 </script>
- 23 </head>
- 24 <body>
- 25 <button id="bt1">开始</button>
- 26 <div id="div2">我是内容</div>
- 27 </body>
- 28 </html>
jQuery animate() - 使用队列功能,jQuery 会创建包含这些方法调用的"内部"队列。然后逐一运行这些 animate 调用。
- 1 <!DOCTYPE html>
- 2 <html lang="en">
- 3 <head>
- 4 <meta charset="UTF-8">
- 5 <title>队列动画</title>
- 6 <style>
- 7 *{margin:0;padding:0;}
- 8 #div2{width:100px;height:100px;background:orange;position:absolute;}
- 9 </style>
- 10 <!-- jquery自己引入就好,我这里的是jquery-1.12.0.min.js -->
- 11 <script type="text/javascript" src="jquery-1.12.0.min.js"></script>
- 12 <script type="text/javascript">
- 13 $(document).ready(function(){
- 14 $("#bt1").click(function(){
- 15 var div=$('#div2');
- 16 div.animate({height:'300px',opacity:'0.4'});
- 17 div.animate({width:'300px',opacity:'0.8'});
- 18 div.animate({height:'100px',opacity:'0.4'});
- 19 div.animate({width:'100px',opacity:'0.8'});
- 20 })
- 21 });
- 22 </script>
- 23 </head>
- 24 <body>
- 25 <button id="bt1">开始</button>
- 26 <div id="div2">我是内容</div>
- 27 </body>
- 28 </html>
jQuery stop() 方法用于在动画或效果完成前对它们进行停止。
jQuery stop() 方法用于停止动画或效果,在它们完成之前。
stop() 方法适用于所有 jQuery 效果函数,包括滑动、淡入淡出和自定义动画。
- 1 <!DOCTYPE html>
- 2 <html lang="en">
- 3 <head>
- 4 <meta charset="UTF-8">
- 5 <title>停止动画</title>
- 6 <style>
- 7
- 8 *{margin:0;padding:0;}
- 9 #div2{width:100px;height:100px;background:orange;position:absolute;}
- 10 </style>
- 11 <!-- jquery自己引入就好,我这里的是jquery-1.12.0.min.js -->
- 12 <script type="text/javascript" src="jquery-1.12.0.min.js"></script>
- 13 <script type="text/javascript">
- 14 $(document).ready(function(){
- 15 $("#bt1").click(function(){
- 16 $("#div2").animate({
- 17 left:'500px',
- 18 top:'150px',
- 19 opacity:'0.5',
- 20 },3000);
- 21 });
- 22 $("#bt2").click(function(){
- 23 $("#div2").stop();
- 24 })
- 25 });
- 26 </script>
- 27 </head>
- 28 <body>
- 29 <button id="bt1">开始</button><button id="bt2">停止</button>
- 30 <div id="div2">我是内容</div>
- 31 </body>
- 32 </html>
更多时候jQuery的动画会涉及函数,也就是会使用一个参数的作用,这也是jQuery动画受青睐的一个原因。例如:
- 1 <!DOCTYPE html>
- 2 <html lang="en">
- 3 <head>
- 4 <meta charset="UTF-8">
- 5 <title>动画</title>
- 6 <style>
- 7
- 8 *{margin:0;padding:0;}
- 9 #div2{width:100px;height:100px;background:orange;position:absolute;}
- 10 </style>
- 11 <!-- jquery自己引入就好,我这里的是jquery-1.12.0.min.js -->
- 12 <script type="text/javascript" src="jquery-1.12.0.min.js"></script>
- 13 <script type="text/javascript">
- 14 $(document).ready(function(){
- 15 $("#bt1").click(function(){
- 16 $("#div2").hide(1000,function(){
- 17 alert("内容被隐藏了");
- 18 })
- 19 })
- 20 });
- 21 </script>
- 22 </head>
- 23 <body>
- 24 <button id="bt1">点击</button>
- 25 <div id="div2">我是内容</div>
- 26 </body>
- 27 </html>