Sample Code of JS uniform motion demonstration

Source: Internet
Author: User

Copy codeThe Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> uniform motion demonstration </title>
<Style type = "text/css">
<! --

# Div1 {width: 1px; height: 200px; background-color: black; position: absolute; left: 100px; top: 70px; float: left}
# Div1 span {position: absolute; top:-15px}
# Div2 {width: 1px; height: 200px; background-color: midnightblue; position: absolute; left: 800px; top: 70px; float: left}
# Div2 span {position: absolute; top:-15px}
# Div3 {width: 1px; height: 200px; background-color: fuchsia; position: absolute; left: 300px; top: 70px; float: left}
# Div3 span {position: absolute; top:-15px}
# Div4 {width: 1px; height: 200px; background-color: darkmagenta; position: absolute; left: 500px; top: 70px; float: left}
# Div4 span {position: absolute; top:-15px}
# Grap {width: 200px; height: 200px; background: red; position: absolute; left: 300px; top: 74px; float: left ;}
Input {width: 100px; margin-left: 90px ;}
-->
</Style>
<Script type = "text/javascript">
Var timeId
Function startMove (target ){
Var oDiv = document. getElementById ('grap ')
ClearInterval (timeId); // it is important to prevent multiple clicks from accelerating.
Speed = oDiv. offsetLeft <target? 8:-9;
TimeId = setInterval (function (){
If (Math. abs (oDiv. offsetLeft-target) <= 6 ){
ODiv. style. left = target + 'px '; // as long as the rectangle moves closer to the target point, it is directly moved to the target point, and the speed change cannot be noticed by the naked eye.
ClearInterval (timeId); document. title = "target" + oDiv. style. left;
}
Else {
ODiv. style. left = oDiv. offsetLeft + speed + 'px ';
}
}, 30 );
}
</Script>
</Head>
<Body>
<Div id = "div1"> <span> 100px </span> </div>
<Div id = "div2"> <span> 800px </span> </div>
<Div id = "div3"> <span> 300px </span> </div>
<Div id = "div4"> <span> 500px </span> </div>
<Input type = "button" value = "move to 100px" onclick = "startMove (100)"/>
<Input type = "button" value = "move to 300px" onclick = "startMove (300)"/>
<Input type = "button" value = "move to 500px" onclick = "startMove (500)"/>
<Input type = "button" value = "move to 800px" onclick = "startMove (800)"/>
<Div id = "grap"> </div>

</Body>
</Html>

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.