JS Buffered Motion

Source: Internet
Author: User

The buffered motion of JS, often used for carousel

<!     DOCTYPE html>#box {width:100px;            height:100px;            Background:skyblue;        Position:absolute; }    </style>varOBox = document.getElementById ("box"); Startmove (OBox, {left:500},function() {Startmove (OBox, {top:500})        }); //you can get a non-inline style        functionGetStyle (obj, attr) {returnObj.currentstyle? OBJ.CURRENTSTYLE[ATTR]: getComputedStyle (obj,NULL) [attr]; }functionstartmove (obj, JSON, fn) {//clears the timer, avoids multiple calls and causes the timer to perform Chaosclearinterval (Obj.timer); Obj.timer= SetInterval (function(){                 for(attrinchJSON) {                    varIcur = 0; //attr If it is opacity, consider the compatible wording of IEIcur = (attr = = ' opacity ')? Math.Round (GetStyle (obj, attr) * 100): parseint (GetStyle (obj, attr)); //ispeed is a growth variable, the divisor can be seen by the situation change                    varIspeed = (json[attr]-icur)/50;//do not understand why Ispeed is greater than 0 rounding up, less than 0 down rounding, may not let Ispeed equals 0Ispeed = ispeed > 0?Math.ceil (ispeed): Math.floor (Ispeed); //Change Style                    if(attr = = ' opacity ') {Obj.style.filter= "Alpha (opacity:" + (Icur+ispeed) + ")"; Obj.style.opacity= (icur+ispeed)/100; }Else{obj.style[attr]= icur + ispeed + ' px '; }//when the value of the style corresponds to the target value, clear the timer, and if there is a callback function that defines FN, execute                    if(Icur = =Json[attr])                        {clearinterval (Obj.timer); FN&&fn (); }                }            }, 10)        }    </script></body>

  

JS Buffered Motion

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.