2016-06-01 jitter

Source: Internet
Author: User

1. Click on the image to achieve jitter

    <script src= "Js/domoveandgetstyle.js" ></script> <script> window.onload=function () {            var img = document.getElementById ("img"); var arr=[];//20-20 18-18 ... 0 Var num=0;//operation Array, generally to use the subscript var timer=null;//cancellation timer with var postion=getstyle (IMG, ' left ');//Get            The current position of the IMG for (var i=20; i>0; i-=2) {Arr.push (i,-i); }//alert (arr)//20-20 18-18 ... 0 img.onclick=function () {//Click on the Picture trigger timer timer=setinterval (function () {Img.style.                    left=postion+arr[num]+ ' px ';                    num++;                    if (num==arr.length) {//If to the end of the array, clear the timer clearinterval (timer);            }},50); }} </script>

2. Domoveandgetstyle.js

/** * Created by Ckang on 2016/6/1.*/functionDomove (obj,attr,steplength,target,animationvelocity) {steplength= (GetStyle (obj,attr) <target?steplength:-steplength);//If obj is in a position smaller than the target position, the step takes a positive number, and the inverse takes a negative numberclearinterval (Obj.timer); Odiv.timer=setinterval (function(){        varSpeed =getstyle (obj,attr) +steplength;//Step Size        if(Speed>target && steplength>0 | | speed<target && steplength<0) { speed=Target; } Obj.style[attr]=speed+ ' px ';//move steplength px every animationvelocity seconds        if(Speed==target) {//Stop Movingclearinterval (Obj.timer); }},animationvelocity);}functionGetStyle (obj,attr) {//why parseint (), because Obj.currentstyle[attr] got the 30px-->30 get the value of the attribute of the element such as position information    returnparseint (Obj.currentstyle?Obj.currentstyle[attr]:getcomputedstyle (obj) [attr]);}

2016-06-01 jitter

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.