animate() method
jQuery animate() method is used to create custom animations.
Syntax:
$(selector).animate({params},speed,callback);
Required params parameter definition to form animation CSS properties.
The optional speed parameter specifies the duration of the effect. It can take the following values: "slow", "fast", or milliseconds.
The optional callback parameter is the name of the function to be executed after the animation is completed.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ $("div").animate({left:'250px'}); }); }); </script> </head> <body> <button>开始动画</button> <p>向右移动</p> <div style="background:#98bf21;height:100px;width:100px;position:absolute;"> </div> </body> </html>
Note:
By default, all HTML elements have a static position and cannot be moved.
If you need to operate the position, remember to first set the CSS position property of the element to relative, fixed or absolute!
animate() - Manipulate multiple properties
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ $("div").animate({ left:'250px', opacity:'0.5', height:'150px', width:'150px' }); }); }); </script> </head> <body> <button>开始动画</button> <p>逐渐变大</p> <div style="background:#98bf21;height:100px;width:100px;position:absolute;"> </div> </body> </html>
Can I use the animate() method to manipulate all CSS properties?
Yes, almost! However, there is one important thing to remember: when using animate(), you must use Camel notation to write all property names, for example, you must use paddingLeft instead of padding-left, use marginRight instead of margin-right, etc. . Also, color animation is not included in the core jQuery library.
animate() - uses relative values
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ $("div").animate({ left:'250px', height:'+=150px', width:'+=150px' }); }); }); </script> </head> <body> <button>开始动画</button> <br><br> <div style="background:#98bf21;height:100px;width:100px;position:absolute;"> </div> </body> </html>
animate() - uses predefined values
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ $("div").animate({ height:'toggle' }); }); }); </script> </head> <body> <button>开始动画</button> <p>收起</p> <div style="background:#98bf21;height:100px;width:100px;position:absolute;"> </div> </body> </html>
animate() - uses queue functions
By default, jQuery provides queue functionality for animations.
This means that if you write multiple animate() calls after each other, jQuery will create an "internal" queue containing those method calls. Then run these animate calls one by one.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ var div=$("div"); div.animate({height:'300px',opacity:'0.4'},"slow"); div.animate({width:'300px',opacity:'0.8'},"slow"); div.animate({height:'100px',opacity:'0.4'},"slow"); div.animate({width:'100px',opacity:'0.8'},"slow"); }); }); </script> </head> <body> <button>开始动画</button> <p></p> <div style="background:#98bf21;height:100px;width:100px;position:absolute;"> </div> </body> </html>