jquery Source Code Analysis---motion animate preprocessing

Source: Internet
Author: User

Before we understand what preprocessing is, let's look at a small example

<! DOCTYPE html>
<
HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <title>Document</title> <style>#div1{width:300px;Height:300px;background:Red; } </style></Head><Body><DivID= "Div1"></Div><Scriptsrc= "Jquery-2.0.3.js"></Script><Script>$( function(){ $('#div1'). Animate ({width:'50%'}, -);} );</Script></Body></HTML>

You can see that when I change the width of the writing is 50%, and the initial width is 300px, then how to convert the two? This involves a preprocessing problem.

In line 7860, jquery defines a variable called tweeners,

In 7864 rows, the variable parts is used to receive a regular match to the value, that is, 50 and%

In 7865 rows, the variable unit is used to receive units, which is the PX

The next step is to convert the unit, which is a little bit more complicated to write.

The idea is to convert the 20px to a percentage first, for example, in this case, it is converted to 1.4% and then to 50%.

So what are some interesting ways to do it in sports? We continue to

<!DOCTYPE HTML><HTMLLang= "en"><Head>    <MetaCharSet= "UTF-8">    <title>Document</title>    <style>#div1{width:300px;Height:300px;background:Red;position:Absolute; }    </style></Head><Body><inputtype= "button"value= "Tap"><DivID= "Div1"></Div><Scriptsrc= "Jquery-2.0.3.js"></Script><Script>$( function(){    $('input'). Click (function() {        $('#div1'). Animate ({width:'+=100'},  -);    }); } );</Script></Body></HTML>

In this example, we use the + = notation, that is, each click will move 100px

The implementation of this part in the source code is also relatively simple

The principle is that after the end to determine whether there is still accumulation, if not accumulated directly to the parts[2] to pay the initial value is OK

Continue, when we write this, the run is OK

function () {    $ (' input '). Click (function() {        $ (' #div1 '). Animate ({' Margin-left ': 100},         });    } );

Or you can write like this.

function () {    $ (' input '). Click (function() {        $ (' #div1 '). Animate ({' MarginLeft ': 100}, 500 );        });    } );

The reason for this is that a compound style is handled in 8055 rows.

Not to be continued ...

jquery Source Code Analysis---motion animate preprocessing

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.