JS Motion Basics 1

Source: Internet
Author: User

1. In this example, if the Div block movement continues to click the button, the speed will be more and more quickly, so the other timer to open the time to turn off the previous timer. Here the timer defines the position will affect whether the timer is closed, if the timer is defined as a local variable, then every time the click is null, then the clearinterval does not play a role in the shutdown timer. Therefore, the timer must be defined as a global variable.

2. For speed this variable, no, he put it outside, directly as a local variable.

<!DOCTYPE HTML><HTML><HeadLang= "en">    <MetaCharSet= "UTF-8">    <title></title>    <style>#div1{width:200px;Height:300px;background:#ffccdd;position:Absolute;        }    </style></Head><Body><inputID= "BTN"type= "button"/><DivID= "Div1"></Div><Script>    varobtn=document.getElementById ('btn'); varOdiv=document.getElementById ('Div1'); varTimer= NULL; Obtn.onclick= function(){//var timer = null;clearinterval (timer); Timer=SetInterval (function (){        var Speed= 1; if(Odiv.offsetleft> -) {clearinterval (timer); }         Else{oDiv.style.left=Odiv.offsetleft+ Speed+'px'; }        }, -); };</Script></Body></HTML>

JS Motion Basics 1

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.