Write a simple motion template

Source: Internet
Author: User

<!DOCTYPE HTML><HTMLLang= "en"><Head>    <MetaCharSet= "UTF-8">    <Metaname= "Viewport"content= "Width=device-width, initial-scale=1.0">    <Metahttp-equiv= "X-ua-compatible"content= "Ie=edge">    <title>Simple exercise</title>    <style>Div{width:50px;Height:50px;background:#CC66CC;Margin-bottom:20px;position:relative;        }    </style></Head><Body>     <!--Two Div uses a motion function, one moves to the right 350px, one moves to the right 590px, each move 3px -      <inputtype= "button"value= "Start Motion">      <DivID= "Div1"></Div>      <DivID= "Div2"></Div>      <Script>window.onload= function(){              varbtn=Document.queryselector ("input"); varDiv1=Document.queryselector ("#div1"); varDiv2=Document.queryselector ("#div2"); vartimer; functionMove (Obj,attr,target) {clearinterval (Obj.timer); Obj.timer=SetInterval (function(){                      varVal=parseint (getComputedStyle (obj) [attr]); Val+=3; if(Val>=target)                          {clearinterval (Obj.timer); Val=Target; } Obj.style[attr]=Val+"px"; }, -); } Btn.onclick= function() {Move (DIV1," Left", -); Move (Div2," Left",590); }                        }      </Script></Body></HTML>

1. When using the timer, it is necessary to clear the other timers according to the situation, to avoid multiple clicks of the property value changes faster.

2. Revise the value of the last movement stop, and note the relationship between the moving target value and the change value (target, Val in the code).

Write a simple motion template

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.