-
Notifications
You must be signed in to change notification settings - Fork 1
/
17. Source Code.txt
132 lines (122 loc) · 4.2 KB
/
17. Source Code.txt
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
Source Code
<html>
<head>
<script>
let myBlock;
let myFunctionList;
let funList = [];
const movementArray = ["right", "left", "up", "down"];
document.addEventListener("DOMContentLoaded", function () {
myBlock = document.createElement("div");
myBlock.textContent = "hello world";
myBlock.style.width = "100px";
myBlock.style.height = "100px";
myBlock.style.backgroundColor = "red";
myBlock.style.color = "white";
myBlock.style.lineHeight = "100px";
myBlock.style.textAlign = "center";
myBlock.style.position = "absolute";
myBlock.style.top = "100px";
myBlock.style.left = "150px";
document.body.appendChild(myBlock);
myFunctionList = document.createElement("div");
document.body.appendChild(myFunctionList);
})
document.addEventListener("keydown", function (e) {
e.preventDefault();
let keyC = e.keyCode;
if (keyC === 37) {
addFun("left");
}
else if (keyC === 39) {
addFun("right");
}
else if (keyC === 38) {
addFun("up");
}
else if (keyC === 40) {
addFun("down");
}
else if (keyC === 67) {
myBlock.style.backgroundColor = randomColor();
}
else if (keyC === 82) {
let temp = movementArray[Math.floor(Math.random() * movementArray.length)];
addFun(temp);
}
else if (keyC === 13 || keyC === 32) {
mover();
}
console.log(e.keyCode);
})
function mover() {
if (funList.length > 0) {
let cur = myBlock.getBoundingClientRect();
let el = funList.shift();
let item = el.textContent.replace("+", "");
myFunctionList.removeChild(el);
myBlock.innerHTML = "Move:" + item;
if (item == "left") {
myBlock.style.left = cur.left - cur.width + "px";
}
if (item == "right") {
myBlock.style.left = cur.left + cur.width + "px";
}
if (item == "up") {
myBlock.style.top = cur.top - cur.height + "px";
}
if (item == "down") {
myBlock.style.top = cur.top + cur.height + "px";
}
setTimeout(mover, 300);
}
else {
myBlock.innerHTML = "Set Path";
return;
}
}
function addFun(val) {
let span = document.createElement("span");
span.textContent = "+" + val;
span.style.padding = "10px";
span.style.border = "1px solid #ddd";
span.addEventListener("mouseover", function () {
this.style.backgroundColor = "red";
this.style.color = "white";
})
span.addEventListener("mouseout", function () {
this.style.backgroundColor = "white";
this.style.color = "black";
})
myFunctionList.appendChild(span);
funList.push(span);
console.log(funList);
//myFunctionList
}
function randomColor() {
return "#" + Math.random().toString(16).substr(-6);
}
function goLeft() {
let temp = myBlock.offsetLeft;
temp = temp - 50;
myBlock.style.left = temp + "px";
}
function goRight() {
let temp = myBlock.offsetLeft;
temp = temp + 50;
myBlock.style.left = temp + "px";
}
function goUp() {
let temp = myBlock.offsetTop;
temp = temp - 50;
myBlock.style.top = temp + "px";
}
function goDown() {
let temp = myBlock.offsetTop;
temp = temp + 50;
myBlock.style.top = temp + "px";
}
</script>
</head>
<body> </body>
</html>