-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyunsu2.html
More file actions
61 lines (48 loc) · 1.32 KB
/
yunsu2.html
File metadata and controls
61 lines (48 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>运动框架---匀速运动2---终止条件(范围)</title>
</head>
<style>
#div1{
width: 200px;
height: 200px;
position: absolute;
background-color: chocolate;
left: 0px;
top: 200px;
}
</style>
<script>
var timmer = null;
function startMove(target){
var oDiv = document.getElementById("div1");
clearInterval(timmer);
timmer = setInterval(function(){
var speed = 0;
if(oDiv.offsetLeft<target){
speed = 7;
}else{
speed = -7;
}
//匀速运动的终止条件是距离足够近(在一定范围内便到达)
if(Math.abs(oDiv.offsetLeft-target)<7){
clearInterval(timmer);
oDiv.style.left = target+'px';
}else{
oDiv.style.left = oDiv.offsetLeft+speed+"px";
}
},30);
}
</script>
<body>
<p>
<h3>匀速运动的停止条件</h3>
<h4>距离足够近,在一定范围内即可</h4>
</p>
<input type="button" value="MOVE" onclick="startMove(500)">
<div id="div1"></div>
<span style="width: 1px;height: 300px;left: 500px;position: absolute;background-color: black" ></span>
</body>
</html>