-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmovingbox.html
More file actions
87 lines (77 loc) · 1.52 KB
/
movingbox.html
File metadata and controls
87 lines (77 loc) · 1.52 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
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
<style>
* {
margin: 0;
padding: 0;
}
#box {
position: fixed;
top: 200px;
left: 200px;
width: 100px;
height: 100px;
background-color: yellow;
transition: all 0.5s;
}
#ctrlcont {
position: fixed;
width: 250px;
height: 170px;
top: 10px;
right: 20px;
}
.ctrl {
position: absolute;
display: block;
width: 80px;
height: 80px;
background-color: green;
}
#up {
left: 90px
}
#down {
top: 90px;
left: 90px;
}
#left {
top: 90px;
}
#right {
top: 90px;
left: 180px;
}
</style>
<div id="box"></div>
<div id="ctrlcont">
<div id="up" class="ctrl"></div>
<div id="down" class="ctrl"></div>
<div id="left" class="ctrl"></div>
<div id="right" class="ctrl"></div>
</div>
<script>
var box = document.getElementById('box');
var up = document.getElementById('up');
var down = document.getElementById('down');
var left = document.getElementById('left');
var right = document.getElementById('right');
up.onclick = function(){
var x = box.getBoundingClientRect();
var moveup = x.top - 50;
box.style.top = moveup + 'px';
}
down.onclick = function(){
var x = box.getBoundingClientRect();
var movedown = x.top + 50;
box.style.top = movedown + 'px';
}
left.onclick = function(){
var x = box.getBoundingClientRect();
var moveleft = x.left - 50;
box.style.left = moveleft + 'px';
}
right.onclick = function(){
var x = box.getBoundingClientRect();
var moveright = x.left + 50;
box.style.left = moveright + 'px';
}
</script>