Javascript chained motion framework-line-by-line code analysis allows you to easily understand the principle of motion

Source: Internet
Author: User

Javascript chained motion framework-line-by-line code analysis allows you to easily understand the principle of motion

The so-called chain movement is a link. In fact, many of our sports refer to stages. The first stage is complete and the next stage starts to change. This chained motion frame is used to solve these problems.

Let's take a look at the previous motion framework. The following is the Javascript code.

function getStyle(obj, name) {    if (obj.currentStyle) {        return obj.currentStyle[name];    } else {        return getComputedStyle(obj, null)[name];    }}function startMove(obj, attr, iTarget) {    clearInterval(obj.time);    obj.time = setInterval(function() {        var cur = 0;        if (attr == 'opacity') {            cur = Math.round(parseFloat(getStyle(obj, attr)) * 100);        } else {            cur = parseInt(getStyle(obj, attr));        }        var speed = (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, it is equivalent to a stage-type motion frame. When an object moves somewhere, it stops. So how can we achieve Chain movement?

We are adding a fnEnd parameter. This is a function that will be called at the end of the motion.

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

The principle is: a motion is completed at the start of the next motion.

In this way, we can complete the chain movement. Let's take a look at the code.

<script src="js/move_lianshi.js" type="text/javascript" charset="utf-8"></script>        <script type="text/javascript">            window.onload=function(){                var oDiv=document.getElementById("div1");                oDiv.onmouseover=function(){                    startMove(oDiv,'width',300,function(){                        startMove(oDiv,'height',300);                    });                };            }        </script>                            

Javascript:

function getStyle(obj, name) {    if (obj.currentStyle) {        return obj.currentStyle[name];    } else {        return getComputedStyle(obj, null)[name];    }}function startMove(obj, attr, iTarget,fnEnd) {    clearInterval(obj.time);    obj.time = setInterval(function() {        var cur = 0;        if (attr == 'opacity') {            cur = Math.round(parseFloat(getStyle(obj, attr)) * 100);        } else {            cur = parseInt(getStyle(obj, attr));        }        var speed = (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);}

In this way, the Div will first become wider and higher.

We still have limitations in this motion frame. What is that?

So what should I do if I want the Div to become wider and wider at the same time? In the next update, we will solve this problem and launch a perfect motion frame that supports most movements.

Coming soon ~

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.