-
Notifications
You must be signed in to change notification settings - Fork 0
/
task13.html
104 lines (88 loc) · 2.69 KB
/
task13.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Javascript - Task 7</title>
<style>
#train {
width: 400px;
height: 200px;
position: relative;
}
.track {
width: 100%;
float: left;
}
.start {
padding: 4px 20px;
background-color: green;
color: white;
cursor: pointer;
}
.stop {
padding: 4px 20px;
background-color: red;
color: white;
cursor: pointer;
}
.control {
text-align: center;
}
.screen {
text-align: center;
padding-top: 30px;
}
</style>
</head>
<body>
<div class="track" style="padding-top: 10%;">
<img id="train" src="./train.png" />
</div>
<div class="control" style="padding-top: 20%;">
<span class="start" onclick="return startTrain();">Start</span>
<span class="stop" onclick="return stopTrain();">Stop</span>
</div>
<script>
let running_status = "stop";
let moving_type = "left";
let starter = 0;
let race = 0;
let run;
engine();
function engine() {
run = setInterval(function () {
if (running_status == "start") {
let control = (race - starter) * 20;
let reached = control / 5;
if (moving_type == "left") {
train.style.left = reached + "px";
} else {
train.style.right = reached + "px";
}
if (reached > 1200) {
if (moving_type == "left") {
train.style.float = "right";
train.style.left = "initial";
moving_type = "right";
} else {
train.style.float = "left";
moving_type = "left";
train.style.right = "initial";
}
starter = 0;
race = 0;
}
race++;
}
}, 20);
}
function startTrain() {
running_status = "start";
}
function stopTrain() {
running_status = "stop";
}
</script>
</body>
</html>