1. animate syntax structure
animate(params,speed,callback)
params: a mapping containing style attributes and values, such as {key1:value1,key2:value2}
speed: Speed parameter [optional]
callback: function executed when the animation is completed [optional]
2. Customize simple animation
Use a simple example To illustrate, click on a div to achieve the effect of horizontal floating on the page.
<style> #cube{ position:relative;/* 不加这句元素不能动 */ width:30px; height:30px; background:red; cursor:pointer; } </style> <body> <div> <div id="cube"></div> </div> <script> $(function(){ $("#cube").click(function(){ $(this).animate({left:"100px"},2000) }) }) </script>
In order to make the element move, you need to change the left attribute. In order to affect the element's top, right, bottom, and left attribute values, the element's position must be declared.
3. Accumulation and Accumulation Subtraction Animation
In the previous code, the attribute {left: "100px"} was set as a parameter , if rewritten as {left:"+=25px"}, the effect is as follows
##4. Multiple animations$(this).animate({left:"+=25px",height:"+=20px"},1000)
##5. Execute multiple animations in sequence
In the above example, sliding to the right and increasing the height occur at the same time. If you want the box to slide to the right first and then become taller, you only need to split the code
as follows:
$(this).animate({left:"+=25px"},500) .animate({height:"+=20px"},500)
The execution of animation effects like this has a sequence, which is called "animation queue"
## 6. Comprehensive Example
Click the square to make it move to the right while increasing its height and opacity from 50% to 100%, then move it up and down, widen, and fade out when finished.
$("#cube").click(function(){ $(this).animate({left:"100px",height:"100px",opacity:"1"},500) .animate({top:"40px",width:"100px"},500) .fadeOut('slow') })
7. Animation callback function
In the above example, if you want to switch the css style (background: blue) in the last step, instead of Fade out, if handled as usual, the relevant code is as follows:
$("#cube").click(function(){ $(this).animate({left:"100px",height:"100px",opacity:"1"},500) .animate({top:"40px",width:"100px"},500) .css("border","5px solid blue")//改动这行 })
The reason for this problem is that the css() method will not be added to the animation queue, but will be executed immediately. You can use callback functions to implement queuing for non-animated methods.
The correct relevant code is as follows:$("#cube").click(function(){ $(this).animate({left:"100px",height:"100px",opacity:"1"},500) .animate({top:"40px",width:"100px"},500,function(){ $(this).css("border","5px solid blue") }) })
It is worth noting that callback applies to all jquery animation methods, such as slideDown(), show(), etc.
SummaryThe above are several usages and precautions about animate in jquery. I hope the content of this article can bring some help to everyone's study or work. If you have any questions, you can leave a message. comminicate. Thank you for your support to the PHP Chinese website. For more articles related to the usage and precautions of animate in jQuery, please pay attention to the PHP Chinese website!