-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmove3.html
74 lines (73 loc) · 1.38 KB
/
move3.html
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
62
63
64
65
66
67
68
69
70
71
72
73
74
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>运动3</title>
<style type="text/css">
#div_1{
width: 150px;
height: 150px;
position: absolute;
left: 700px;
top: 50px;
background: red;
}
#div_2{
width: 1px;
height: 250px;
position: absolute;
left: 150px;
top: 0px;
background: black;
}
#div_3{
width: 1px;
height: 250px;
position: absolute;
left: 400px;
top: 0px;
background: black;
}
</style>
<script type="text/javascript">
//匀速运动防抖动
var timer=null;
window.onload=function(){
var oBtn1=document.getElementById('button_1');
var oBtn2=document.getElementById('button_2');
oBtn1.onclick=function(){
startMove(150);
}
oBtn2.onclick=function(){
startMove(400);
}
}
function startMove(flag){
var oDiv=document.getElementById('div_1');
clearInterval(timer);
timer=setInterval(function(){
if(oDiv.offsetLeft<flag){
speed=7;
}
else{
speed=-7;
}
if(Math.abs(oDiv.offsetLeft-flag)<7){
clearInterval(timer);
oDiv.style.left=flag+'px';
}
else{
oDiv.style.left=oDiv.offsetLeft+speed+'px';
}
},30);
}
</script>
</head>
<body>
<input id="button_1" type="button" value="150运动">
<input id="button_2" type="button" value="300运动">
<div id="div_1"></div>
<div id="div_2"></div>
<div id="div_3"></div>
</body>
</html>