Skip to content

Commit 0df72fc

Browse files
author
Maksumi Murakami
authored
Merge pull request #392 from maksumi/browser-events
Introduction to browser events
2 parents b8bbf4d + 7c0d1ae commit 0df72fc

File tree

23 files changed

+263
-263
lines changed

23 files changed

+263
-263
lines changed

2-ui/2-events/01-introduction-browser-events/01-hide-other/solution.view/index.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77

88
<body>
99

10-
<input type="button" id="hider" value="Click to hide the text" />
10+
<input type="button" id="hider" value="Haz click para desaparecer el texto"/>
1111

12-
<div id="text">Text</div>
12+
<div id="text">Texto</div>
1313

1414
<script>
15-
// Here it doesn't matter how we hide the text,
16-
// could also use style.display:
15+
// Aquí no importa cómo escondamos el texto
16+
// podríamos usar style.display:
1717
document.getElementById('hider').onclick = function() {
1818
document.getElementById('text').hidden = true;
1919
}

2-ui/2-events/01-introduction-browser-events/01-hide-other/source.view/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77

88
<body>
99

10-
<input type="button" id="hider" value="Click to hide the text" />
10+
<input type="button" id="hider" value="Haz click para desaparecer el texto"/>
1111

12-
<div id="text">Text</div>
12+
<div id="text">Texto</div>
1313

1414
<script>
15-
/* your code */
15+
/* Tu código */
1616
</script>
1717

1818
</body>

2-ui/2-events/01-introduction-browser-events/01-hide-other/task.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ importance: 5
22

33
---
44

5-
# Hide on click
5+
# Ocultar con un click
66

7-
Add JavaScript to the `button` to make `<div id="text">` disappear when we click it.
7+
Agrega JavaScript al `button` para hacer que `<div id="text">` desaparezca al clickearlo.
88

9-
The demo:
9+
El demo:
1010

1111
[iframe border=1 src="solution" height=80]
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
Can use `this` in the handler to reference "the element itself" here:
1+
Podemos usar `this` en el handler para referenciar "al propio elemento" aquí:
22

33
```html run height=50
4-
<input type="button" onclick="this.hidden=true" value="Click to hide">
4+
<input type="button" onclick="this.hidden=true" value="Click para ocultar">
55
```

2-ui/2-events/01-introduction-browser-events/02-hide-self-onclick/task.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ importance: 5
22

33
---
44

5-
# Hide self
5+
# Ocultarse
66

7-
Create a button that hides itself on click.
7+
Crea un botón que se oculte a sí mismo al darle un click.
88

99
```online
10-
Like this:
11-
<input type="button" onclick="this.hidden=true" value="Click to hide">
10+
Así:
11+
<input type="button" onclick="this.hidden=true" value="Click para esconder">
1212
```
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
The answer: `1` and `2`.
1+
La respuesta: `1` y `2`.
22

3-
The first handler triggers, because it's not removed by `removeEventListener`. To remove the handler we need to pass exactly the function that was assigned. And in the code a new function is passed, that looks the same, but is still another function.
3+
El primer handler se activa porque no es removido por `removeEventListener`. Para remover el handler necesitamos pasar exactamente la función que fue asignada. Y en el código se pasa una función que luce igual pero es otra función.
44

5-
To remove a function object, we need to store a reference to it, like this:
5+
Para remover un objeto de función necesitamos almacenar una referencia a él, así:
66

77
```js
88
function handler() {
@@ -13,4 +13,4 @@ button.addEventListener("click", handler);
1313
button.removeEventListener("click", handler);
1414
```
1515

16-
The handler `button.onclick` works independently and in addition to `addEventListener`.
16+
El handler `button.onclick` funciona independientemente y en adición a `addEventListener`.

2-ui/2-events/01-introduction-browser-events/03-which-handlers-run/task.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ importance: 5
22

33
---
44

5-
# Which handlers run?
5+
# ¿Qué handlers se ejecutan?
66

7-
There's a button in the variable. There are no handlers on it.
7+
Hay un botón en la variable. No hay handlers en él.
88

9-
Which handlers run on click after the following code? Which alerts show up?
9+
¿Qué handlers se ejecutan con el click después del siguiente código? ¿Qué alertas se muestran?
1010

1111
```js no-beautify
1212
button.addEventListener("click", () => alert("1"));
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11

2-
First we need to choose a method of positioning the ball.
2+
Primero necesitamos elegir un método para posicionar el balón.
33

4-
We can't use `position:fixed` for it, because scrolling the page would move the ball from the field.
4+
No podemos usar `position:fixed` para ello, porque al desplazar la página se movería el balón del campo.
55

6-
So we should use `position:absolute` and, to make the positioning really solid, make `field` itself positioned.
6+
Así que deberíamos usar `position:absolute` y, para que el posicionamiento sea realmente sólido, hacer que `field` sea posicione a sí mismo.
77

8-
Then the ball will be positioned relatively to the field:
8+
Entonces el balón se posicionará en relación al campo:
99

1010
```css
1111
#field {
@@ -16,36 +16,36 @@ Then the ball will be positioned relatively to the field:
1616

1717
#ball {
1818
position: absolute;
19-
left: 0; /* relative to the closest positioned ancestor (field) */
19+
left: 0; /* relativo al predecesor más cercano (field) */
2020
top: 0;
21-
transition: 1s all; /* CSS animation for left/top makes the ball fly */
21+
transition: 1s all; /* Animación CSS para que left/top hagan al balón volar */
2222
}
2323
```
2424

25-
Next we need to assign the correct `ball.style.left/top`. They contain field-relative coordinates now.
25+
Lo siguiente es asignar el `ball.style.left/top` correcto. Ahora contienen coordenadas relativas al campo.
2626

27-
Here's the picture:
27+
Aquí está la imagen:
2828

2929
![](move-ball-coords.svg)
3030

31-
We have `event.clientX/clientY` -- window-relative coordinates of the click.
31+
Tenemos `event.clientX/clientY`, las cuales son las coordenadas del click relativas a la ventana.
3232

33-
To get field-relative `left` coordinate of the click, we can substract the field left edge and the border width:
33+
Para obtener la coordenada `left` del click relativa al campo necesitamos restar el limite izquierdo del campo y el ancho del borde:
3434

3535
```js
3636
let left = event.clientX - fieldCoords.left - field.clientLeft;
3737
```
3838

39-
Normally, `ball.style.left` means the "left edge of the element" (the ball). So if we assign that `left`, then the ball edge, not center, would be under the mouse cursor.
39+
Normalmente `ball.style.left` significa el "borde izquierdo del elemento" (el balón). Por lo que si asignamos ese `left`, entonces el borde del balón, no el centro, es el que se encontraría debajo del cursor del mouse.
4040

41-
We need to move the ball half-width left and half-height up to make it center.
41+
Necesitamos mover la mitad del ancho del balón a la izquierda y la mitad del alto hacia arriba para que quede en el centro.
4242

43-
So the final `left` would be:
43+
Por lo que el `left` final debería ser:
4444

4545
```js
4646
let left = event.clientX - fieldCoords.left - field.clientLeft - ball.offsetWidth/2;
4747
```
4848

49-
The vertical coordinate is calculated using the same logic.
49+
La coordenada vertical es calculada usando la misma lógica.
5050

51-
Please note that the ball width/height must be known at the time we access `ball.offsetWidth`. Should be specified in HTML or CSS.
51+
Por favor, nota que el ancho/alto del balón se debe conocer al momento que accedemos a `ball.offsetWidth`. Se debe especificar en HTML o CSS.

2-ui/2-events/01-introduction-browser-events/04-move-ball-field/solution.view/index.html

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
<body style="height:2000px">
2929

30-
Click on a field to move the ball there.
30+
Haz click en un lugar del campo para mover el balón allí.
3131
<br>
3232

3333

@@ -39,29 +39,29 @@
3939
<script>
4040
field.onclick = function(event) {
4141

42-
// window-relative field coordinates
42+
// Coordenadas del campo relativas a la ventana
4343
let fieldCoords = this.getBoundingClientRect();
4444

45-
// the ball has position:absolute, the field: position:relative
46-
// so ball coordinates are relative to the field inner left-upper corner
45+
// El balón tiene position:absolute, el campo: position:relative
46+
// por lo que las coordenadas de la bola son relativas a la esquina superior izquierda interna del campo
4747
let ballCoords = {
4848
top: event.clientY - fieldCoords.top - field.clientTop - ball.clientHeight / 2,
4949
left: event.clientX - fieldCoords.left - field.clientLeft - ball.clientWidth / 2
5050
};
5151

52-
// prevent crossing the top field boundary
52+
// previniendo el cruce del limite superior del campo
5353
if (ballCoords.top < 0) ballCoords.top = 0;
5454

55-
// prevent crossing the left field boundary
55+
// previniendo el cruce del limite izquierdo del campo
5656
if (ballCoords.left < 0) ballCoords.left = 0;
5757

5858

59-
// // prevent crossing the right field boundary
59+
// // previniendo el cruce del limite derecho del campo
6060
if (ballCoords.left + ball.clientWidth > field.clientWidth) {
6161
ballCoords.left = field.clientWidth - ball.clientWidth;
6262
}
6363

64-
// prevent crossing the bottom field boundary
64+
// previniendo el cruce del limite inferior del campo
6565
if (ballCoords.top + ball.clientHeight > field.clientHeight) {
6666
ballCoords.top = field.clientHeight - ball.clientHeight;
6767
}

2-ui/2-events/01-introduction-browser-events/04-move-ball-field/source.view/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
<body style="height:2000px">
1818

19-
Click on a field to move the ball there.
20-
<br> The ball should never leave the field.
19+
Haz click en un lugar del campo para mover el balón allí.
20+
<br> El balón nunca debe abandonar el campo.
2121

2222

2323
<div id="field">

0 commit comments

Comments
 (0)