Javascript Chain Motion Framework--line by row analysis code, let you easily the principle of motion

Source: Internet
Author: User
Tags setinterval

The so-called chain movement, is tied up. Many of our movements actually refer to stages, the first phase is complete, and the next stage begins to move.

This chain of motion framework is used to deal with these problems.

Let's take a look at the previous motion framework, and here's the JavaScript code

functionGetStyle (obj, name) {if(obj.currentstyle) {returnObj.currentstyle[name]; } Else {        returngetComputedStyle (obj,NULL) [name]; }}functionstartmove (obj, attr, itarget) {clearinterval (obj.time); Obj.time= SetInterval (function() {        varCur = 0; if(attr = = ' opacity ') {cur= Math.Round (parsefloat (GetStyle (obj, attr)) * 100); } Else{cur=parseint (GetStyle (obj, attr)); }        varSpeed = (itarget-cur)/6; speed= Speed > 0?Math.ceil (Speed): Math.floor (speed); if(cur = =itarget)                                {clearinterval (obj.time); } Else {            if(attr = = ' opacity ') {Obj.style.filter= ' Alpha (opacity= ' + cur + speed + ') '; Obj.style.opacity= (cur + speed)/100; } Else{obj.style[attr]= cur + speed + ' px '; }        }    }, 30);}

In fact, he is the equivalent of a stage of the motion frame, an object movement to a place to stop.

So how can we achieve chain-like movement?

We are adding a parameter fnend, this is a function, he will be called at the end of the movement.

Of course, this function can be passed without passing, so we need to make a judgment. It is called only when it is passed in.

The principle is: a movement is completed at the beginning of the next exercise.

This will be able to complete the chain movement, simple, let's look at the code.

<style type= "Text/css" >            #div1{width: 100px; height: 100px;  Background: red;}         </style>
<Scriptsrc= "Js/move_lianshi.js"type= "Text/javascript"CharSet= "Utf-8"></Script>        <Scripttype= "Text/javascript">window.onload=function(){                varOdiv=document.getElementById ("Div1"); Odiv.onmouseover=function() {startmove (Odiv,'width', -,function() {startmove (Odiv,'Height', -);                });            }; }        </Script>    </Head>    <Body>        <DivID= "Div1">                    </Div>    </Body>

Javascript:

functionGetStyle (obj, name) {if(obj.currentstyle) {returnObj.currentstyle[name]; } Else {        returngetComputedStyle (obj,NULL) [name]; }}functionstartmove (obj, attr, itarget,fnend) {clearinterval (obj.time); Obj.time= SetInterval (function() {        varCur = 0; if(attr = = ' opacity ') {cur= Math.Round (parsefloat (GetStyle (obj, attr)) * 100); } Else{cur=parseint (GetStyle (obj, attr)); }        varSpeed = (itarget-cur)/6; speed= Speed > 0?Math.ceil (Speed): Math.floor (speed); if(cur = =itarget)                        {clearinterval (obj.time); if(Fnend) fnend (); } Else {            if(attr = = ' opacity ') {Obj.style.filter= ' Alpha (opacity= ' + cur + speed + ') '; Obj.style.opacity= (cur + speed)/100; } Else{obj.style[attr]= cur + speed + ' px '; }        }    }, 30);}

This way, the div will widen and become taller.

We now have limitations on the framework of the movement, what is it?

So you can't move at the same time, which means I want the div to be big and wide at the same time.

In the next update, we will solve this problem and introduce a perfect motion framework that supports most of the movement.

Please look forward to ~

Javascript Chain Motion Framework--line by row analysis code, let you easily the principle of motion

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.