1 | <!DOCTYPE html> |
2 | <html lang="es"> |
3 | |
4 | <head> |
5 | <meta charset="UTF-8"> |
6 | <meta name="viewport" |
7 | content="width=device-width"> |
8 | <title>Mueve con Botones</title> |
9 | <style> |
10 | .sprite { |
11 | position: fixed; |
12 | font-size: 3rem; |
13 | } |
14 | </style> |
15 | </head> |
16 | |
17 | <body> |
18 | <h1>Mueve con Botones</h1> |
19 | <p> |
20 | <button onclick="retrocede()"> |
21 | ◀ |
22 | </button> |
23 | <button onclick="avanza()"> |
24 | ▶ |
25 | </button> |
26 | </p> |
27 | <output id="carita" |
28 | class="sprite"> |
29 | 😄 |
30 | </output> |
31 | <script> |
32 | let yBase |
33 | let xMenor |
34 | let xMayor |
35 | const VELOCIDAD = 30 |
36 | actualiza() |
37 | let x = xMenor |
38 | dibuja() |
39 | |
40 | function retrocede() { |
41 | actualiza() |
42 | if (x > xMenor) { |
43 | x -= VELOCIDAD |
44 | } |
45 | dibuja() |
46 | } |
47 | |
48 | function avanza() { |
49 | actualiza() |
50 | if (x < xMayor) { |
51 | x += VELOCIDAD |
52 | } |
53 | dibuja() |
54 | } |
55 | |
56 | function actualiza() { |
57 | yBase = innerHeight / 2 |
58 | xMenor = innerWidth / 5 |
59 | xMayor = 4 * innerWidth / 5 |
60 | } |
61 | |
62 | function dibuja() { |
63 | carita.style = `left: ${x}px; |
64 | bottom: ${yBase}px` |
65 | } |
66 | </script> |
67 | </body> |
68 | |
69 | </html> |