复制代码代码如下:
<!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">
<หัว>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>匀速运动演示</title>
<style type="text/css">
-
#div1 {width:1px;height:200px;สีพื้นหลัง: สีดำ;ตำแหน่ง:สัมบูรณ์;ซ้าย:100px;ด้านบน:70px;ลอย:ซ้าย}
#div1 ขยาย {ตำแหน่ง: แน่นอน; บน: -15px}
#div2 {width:1px;height:200px;สีพื้นหลัง:midnightblue;ตำแหน่ง:สัมบูรณ์;ซ้าย:800px;ด้านบน:70px;ลอย: ซ้าย}
#div2 ขยาย {ตำแหน่ง: แน่นอน; บน: -15px}
#div3 {width:1px;height:200px;สีพื้นหลัง:บานเย็น;ตำแหน่ง:สัมบูรณ์;ซ้าย:300px;ด้านบน:70px;ลอย: ซ้าย}
#div3 ขยาย {ตำแหน่ง: แน่นอน; บน: -15px}
#div4 {width:1px;height:200px;สีพื้นหลัง:darkmagenta;ตำแหน่ง:สัมบูรณ์;ซ้าย:500px;ด้านบน:70px;ลอย: ซ้าย}
#div4 ขยาย {ตำแหน่ง: แน่นอน; บน: -15px}
#grap {width:200px;height:200px;พื้นหลัง:สีแดง;ตำแหน่ง:สัมบูรณ์;ซ้าย:300px;ด้านบน:74px;ลอย: ซ้าย;}
อินพุต {ความกว้าง:100px;ระยะขอบซ้าย: 90px;}
-
</สไตล์>
<script type="text/javascript">
var timeId
ฟังก์ชั่น startMove (เป้าหมาย) {
var oDiv=document.getElementById('grap')
clearInterval(รหัสเวลา); //这个地方就是为了防止多次点击速度加快的现象,很重要
ความเร็ว=oDiv.offsetLeft<target?8:-9;
timeId=setInterval(ฟังก์ชั่น(){
ถ้า(Math.abs(oDiv.offsetLeft-เป้าหมาย)<=6){
oDiv.style.left=target+'px'; //只要矩形移动到接近到目标点处就直接移动到目标点上,肉眼无法察觉速度变化
clearInterval(รหัสเวลา); document.title="目标"+oDiv.style.left;
-
อื่น{
oDiv.style.left=oDiv.offsetLeft+ความเร็ว+'px';
-
},30);
-
</สคริปต์>
</หัว>
<ร่างกาย>
<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="移动到100px处" onclick="startMove(100)"/>
<input type="button" value="移动到300px处" onclick="startMove(300)"/>
<input type="button" value="移动到500px处" onclick="startMove(500)"/>
<input type="button" value="移动到800px处" onclick="startMove(800)"/>
<div id="grap" ></div>
</ร่างกาย>
</html>