Usage and precautions of animate in jQuery

Source: Internet
Author: User
Before today, I still focused on the most common usage of the animate () method in jQuery. I checked the manual and found that several useful callback functions were added at 1.8, let's take a look at the usage and precautions of animate in jQuery, so that you can refer to it when you need it. Let's take a look at it. I. animate syntax structure

animate(params,speed,callback)

Params: A ing containing style attributes and values, such as {key1: value1, key2: value2}

Speed: speed parameter [Optional]

Callback: function executed when the animation is completed [Optional]

Ii. Custom simple animation

A simple example is used to illustrate the effect of clicking a p on the page.

 

Script $ (function () {$ ("# cube "). click (function () {$ (this ). animate ({left: "100px"}, 2000)}) script

To make the element move, you need to change the left attribute. In order to affect the attribute values of top, right, bottom, and left elements, the position of the element must be declared.

In the above example, sliding to the right and increasing the height occur at the same time. If you want the square to slide to the right and then increase it, you only need to split the code.

As follows:

$(this).animate({left:"+=25px"},500)  .animate({height:"+=20px"},500)

The execution of an animation effect such as this is sequential, called "animation queue"

Click the square to move it to the right and increase it at the same time. The opacity increases from 50% to 100%. Then it moves up and down, widening, and fades out after completion.

$("#cube").click(function(){    $(this).animate({left:"100px",height:"100px",opacity:"1"},500)    .animate({top:"40px",width:"100px"},500)    .fadeOut('slow')   })

When multiple effects are applied to the same element, these effects can be queued in a chained manner.

In the above example, if you want to switch the css style (background: blue) in the last step, instead of fading out, 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") // change this line })

However, the css () method is called in advance.

This problem occurs because the css () method is executed immediately instead of being added to the animation queue. You can use the callback function to queue non-animated methods.

The correct 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 s0000down () and show.

Summary

The above are some usage and notes about animate in jquery. I hope the content in this article will help you in your study or work. If you have any questions, please leave a message. Thank you for your support for PHP.

For more information about the usage and precautions of animate in jQuery, refer to the PHP Chinese website!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.