Several usages and cautions of animate in jquery _jquery

Source: Internet
Author: User

I. Grammatical structure of animate

Animate (Params,speed,callback)

Params: A mapping that contains style attributes and values, such as{key1:value1,key2:value2}

Speed: Speed parameter [optional]

Callback: Functions that are executed when the animation completes [optional]

Two, custom simple animation

Use a simple example to illustrate the effect of clicking a div across the page.

<style>
 #cube {
  position:relative;/* does not add this sentence element cannot move
  /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"}, )})
 </script>

To make the element move, change the left property. To be able to affect the element top, right, bottom, left property value must declare the element's position.


Demo effect

Third, cumulative, tired minus animation

In the previous code, {left:"100px"} this property was set as a parameter, and if overridden to, the effect is as {left:"+=25px"} follows


Demo effect

Four, multiple animation

Perform multiple animations at the same time

The example above is a very simple animation. If you want to perform multiple animations at the same time, for example, when the element is sliding to the right, magnify the element height.

The code is as follows:

$ (this). Animate ({left: "+=25px", Height: "+=20px"},1000)

Demo effect

Perform multiple animations in sequence

In the example above, sliding to the right and height is happening at the same time, if you want the square to slide to the right and then higher, just split the code

As follows:

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

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


Demo effect

VI. Integrated Examples

Click the box, let him move to the right while increasing, opacity from 50% to 100%, then move up and down, widen, finish and fade out.

$ ("#cube"). Click (function () {
    $ (this). Animate ({left: "100px", Height: "100px", Opacity: "1"},500)
    . Animate ({ Top: "40px", Width: "100px"},500)
    . fadeout (' Slow ')
   })

These effects can be queued in a chained fashion when multiple effects are applied to the same element.


Demo Effect

Seven, animation callback function

In the example above, if you want to switch the CSS style (Background:blue) in the last step instead of fading out, if you follow the usual process, 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.


Demo effect

This problem is caused by the fact that the css() method is not added to the animation queue, but is executed immediately. You can use callback functions to queue a non-animated method implementation.

The correct related 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")})
   

Demo effect

Notably, callback applies to all of the jquery animation methods, such as slidDown() , and show() so on.

Summarize

The above is about the animate in jquery several uses and notices, hope this article content to everybody's study or work can bring certain help, if has the question everybody may the message exchange. Thank you for your support to the cloud-dwelling community.

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.