Animate () method usage instance in jquery _jquery

Source: Internet
Author: User

This example describes the use of the Animate () method in jquery. Share to everyone for your reference. The specific analysis is as follows:

This method is used to create custom animations and to specify the length and erase effects of animations. You can also trigger a callback function when the animation is complete.

Use of the Animate () method:

Mode one:

defines the animation termination style property as a property name/value object. For example:

Copy Code code as follows:
$ ("div"). Animate ({width: "1000px"})

The above code can adjust the div from the original width to 1000px. You can also use multiple sets of attribute name/value objects at once. For example:

Copy Code code as follows:
$ ("div"). Animate ({width: "1000px", fontsize:20})

The above code can adjust the div from the original width to 1000px, from the original font size to 20px. Special attention needs to be paid to the following three points:

1. If the size does not have units, then the default unit is PX.
2. The attribute value needs to be wrapped in double quotes, which can be omitted if the property value is numeric.
3. Properties such as Font-szie or background-color need to remove the middle horizontal line and capitalize the first letter of the second word.

The animate () method can specify the duration of the animation effect, or use the default value normal if not specified. For example:

Copy Code code as follows:
$ ("div"). Animate ({width: "1000px", fontsize:20},2000)

The above code stipulates that the animation effect is completed after 2000 milliseconds (2 seconds).
You can call the callback function after the animation has finished executing. For example:

Copy Code code as follows:
$ ("div"). Animate ({width: "1000px"},5000,function () {Alert (adjustment complete)})

The code above can trigger the callback function after the animation completes, and a prompt box pops up.
Instance code:

Copy Code code as follows:

<! DOCTYPE html>
<meta charset= "Utf-8" >
<meta name= "Author" content= "http://www.jb51.net/"/>
<title> Cloud-dwelling community </title>
<style type= "Text/css" >
div{
width:150px;
height:150px;
BORDER:1PX solid blue;
}
</style>
<script type= "Text/javascript" src= "Mytest/jquery/jquery-1.8.3.js" ></script>
<script type= "Text/javascript" >
$ (document). Ready (function () {
$ ("button"). Click (function () {
$ ("div"). Animate ({width: "1000px", Fontsize:20},5000,function () {alert ("Adjust Complete")});
});
});
</script>
<body>
<div> small Ant </div>
<button id= "BTN1" > Perform an animation </button>
</body>

Mode two:
In mode one, the curly braces {} are used only when defining the animation termination style properties, followed by the animation speed, the callback function, and so on, and they are separated by commas. In the way we're going to introduce, speed, callback functions, queues, and so on are all placed in braces {}.
For example:

Copy Code code as follows:
$ ("div"). Animate ({width: "1000px"}, {Queue:false, duration:1000,complete:function () {alert ("OK"}})

The queue parameter can specify whether the animation is added to the animation queue execution and, if entered into the animation queue, is performed sequentially, after the first animation completes, the second animation in the queue is executed, and so on. If the queue argument value is True, the animation is added to the queue, or the queue is not joined.
The duration parameter is the duration that defines the animation.
The complete parameter defines the callback function for the animation.
Instance code:

Copy Code code as follows:

<! DOCTYPE html>
<meta charset= "Utf-8" >
<meta name= "Author" content= "http://www.jb51.net/"/>
<title> Cloud-dwelling community </title>
<style type= "Text/css" >
. first{
width:150px;
height:150px;
BORDER:1PX solid blue;
}
. second{
width:150px;
height:150px;
BORDER:1PX solid blue;
}
</style>
<script type= "Text/javascript" src= "Mytest/jquery/jquery-1.8.3.js" ></script>
<script type= "Text/javascript" >
$ (document). Ready (function () {
$ ("#btn1"). Click (function () {
$ (". a"). Animate ({width: "1000px"},{queue:true, duration:5000,complete:function A () {alert ("width set complete")})
. Animate ({fontsize: ' 7em '},{queue:true, duration:5000})
. Animate ({borderwidth:10},{queue:true, duration:5000,complete:function a () {alert ("width setting complete"}});
});

$ ("#btn2"). Click (function () {
$ (". Second"). Animate ({width: "1000px"},{queue:false, duration:1000})
. Animate ({fontsize: ' 7em '}, 1000)
. Animate ({borderwidth:10}, 1000);
})
})
</script>
<body>
<div class= "A" > Welcome to Yun-Habitat community </div>
<div class= "Second" > Welcome to Yun-Habitat community </div>
<button id= "BTN1" > Perform first animation </button>
<button id= "BTN2" > Perform a second animation </button>
</body>

You can compare the Add animation queue and do not join the animation queue to perform the effect.

I hope this article will help you with your jquery programming.

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.