-
Notifications
You must be signed in to change notification settings - Fork 166
/
index.html
41 lines (35 loc) · 1.01 KB
/
index.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
<html>
<SCRIPT>
var pos = 0;
const pacArray = [
['PacMan1.png', 'PacMan2.png'],
['PacMan3.png', 'PacMan4.png']
];
var direction = 0;
var focus = 0;
function Run() {
let img = document.getElementById("PacMan");
let imgWidth = img.width
focus = (focus + 1) % 2;
direction = checkPageBounds(direction, imgWidth);
img.src = pacArray[direction][focus];
if (direction) {
pos -= 20;
img.style.left = pos + "px";
} else {
pos += 20;
img.style.left = pos + 'px';
}
setTimeout(Run, 200);
}
function checkPageBounds(direction, imgWidth) {
let pageWidth = window.innerWidth;
if (direction == 0 && pos + imgWidth > pageWidth) direction = 1;
if (direction == 1 && pos < 0) direction = 0;
return direction;
}
</SCRIPT>
<body>
<img id="PacMan" src="PacMan1.png" width='200' onclick="Run()" style="position:absolute"> </img>
</body>
</html>