No Consigo hacer el efecto de pelota rebotando en una caja
Publicado por victor (3 intervenciones) el 28/11/2017 23:17:39
Me falta que rebote por las paredes pero no consigo acertar con la lógica necesaria
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
<!DOCTYPE html>
<html>
<style>
#container {
width: 400px;
height: 400px;
position: relative;
background: yellow;
}
#animate {
width: 50px;
height: 50px;
position: absolute;
background-color: red;
}
</style>
<body>
<p>
<button onclick="myMove()">Click Me</button>
</p>
<div id ="container">
<div id ="animate"></div>
</div>
<script>
function myMove() {
var elem = document.getElementById("animate");
// var pos = 0;
var id = setInterval(frame, 5);
var sigue = true;
var sigueY = false;
var sigueX = false;
var x = 0;
var y = 0;
function frame() {
//clearInterval(id);
if (sigue) {
y++;
x++;
elem.style.top = y + 'px';
elem.style.left = x + 'px';
} else {
y --;
x--;
elem.style.top = y + 'px';
elem.style.left = x + 'px';
}
}
}
</script>
</body>
</html>
Valora esta pregunta


0