Skip to content

Commit bec50da

Browse files
authored
Merge pull request #188 from pierangelomiceli/bro_details
Introduction to browser events
2 parents 72ffca7 + a04a725 commit bec50da

File tree

13 files changed

+81
-83
lines changed

13 files changed

+81
-83
lines changed

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+
# Nascondere al click
66

7-
Add JavaScript to the `button` to make `<div id="text">` disappear when we click it.
7+
Aggiungi del codice JavaScript al pulsante `button` per nascondere `<div id="text">` al click.
88

9-
The demo:
9+
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+
Qui possiamo usare `this` nel gestore per fare riferimento all'"elemento stesso":
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="Clicca per nascondere">
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+
# Nascondere sé stesso
66

7-
Create a button that hides itself on click.
7+
Creare un pulsante che nasconde sé stesso al click.
88

99
```online
10-
Like this:
11-
<input type="button" onclick="this.hidden=true" value="Click to hide">
10+
Come questo:
11+
<input type="button" onclick="this.hidden=true" value="Clicca per nascondere">
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+
Risposta: `1` e `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+
Il primo gestore verrà innescato, poiché non viene rimosso da `removeEventListener`. Per rimuovere il gestore dobbiamo passare esattamente la stessa funzione che era stata assegnata. E nel codice viene passata una nuova funzione, che è identica, ma è comunque una nuova funzione.
44

5-
To remove a function object, we need to store a reference to it, like this:
5+
Per poter rimuovere un oggetto funzione, dobbiamo salvarci un suo riferimento:
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+
Il gestore `button.onclick` aggiunto ad `addEventListener` funziona perfettamente.

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+
# Quale gestore verrà eseguito?
66

7-
There's a button in the variable. There are no handlers on it.
7+
Nella variabile c'è un pulsante. Non vi sono gestori assegnati.
88

9-
Which handlers run on click after the following code? Which alerts show up?
9+
Dopo aver eseguito questo codice, quali gestori verranno eseguiti al click sul pulsante? Quale alert verrà mostrato?
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+
Per prima cosa dobbiamo scegliere un metodo per posizionare la palla.
33

4-
We can't use `position:fixed` for it, because scrolling the page would move the ball from the field.
4+
Non possiamo usare `position:fixed` per questo, perché lo scorrimento della pagina sposterebbe la palla dal campo.
55

6-
So we should use `position:absolute` and, to make the positioning really solid, make `field` itself positioned.
6+
Dobbiamo quindi usare `position:absolute` per la palla, e per rendere il posizionamento stabile, posizionare anche `field` stesso ("static" se non diversamente specificato).
77

8-
Then the ball will be positioned relatively to the field:
8+
In questo modo a palla verrà posizionata in relazione 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 all'antenato più vicino (field) */
2020
top: 0;
21-
transition: 1s all; /* CSS animation for left/top makes the ball fly */
21+
transition: 1s all; /* Una animazione CSS impostata su left/top fa volare la palla */
2222
}
2323
```
2424

25-
Next we need to assign the correct `ball.style.left/top`. They contain field-relative coordinates now.
25+
Il prossimo passo sarà quello di assegnare correttamente `ball.style.left/top`, le cui coordinate saranno relazionate alla dimensione del campo.
2626

27-
Here's the picture:
27+
Osserviamo la figura:
2828

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

31-
We have `event.clientX/clientY` -- window-relative coordinates of the click.
31+
`event.clientX/clientY` sono le coordinate del click relative alla window (N.d.T. l'oggetto window).
3232

33-
To get field-relative `left` coordinate of the click, we can substract the field left edge and the border width:
33+
Per ottenere le coordinate `left` relative al campo, dobbiamo sottrarre il valore del limite sinistro del campo e la sua larghezza:
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 "limite destro dell'elemento" (la palla). Ma se assegnassimo questo valore di `left`, sotto il puntatore si verrebbe a posizionare, appunto, il limite destro della palla, e non il suo centro.
4040

41-
We need to move the ball half-width left and half-height up to make it center.
41+
Per poter centrare la palla, dobbiamo spostarla tenendo conto della metà della sua altezza e metà della sua larghezza.
4242

43-
So the final `left` would be:
43+
Quindi il `left` definitivo sarebbe:
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+
Le coordinate in verticale vengono calcolate usando la stessa logica.
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+
Nota bene che larghezza e altezza della palla devono essere note nel momento in cui accediamo a `ball.offsetWidth`. Dovrebbero essere specificate nell'HTML o nel CSS.

2-ui/2-events/01-introduction-browser-events/04-move-ball-field/task.md

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

33
---
44

5-
# Move the ball across the field
5+
# Sposta la palla sul campo
66

7-
Move the ball across the field to a click. Like this:
7+
Sposta la palla sul campo al click. Così:
88

99
[iframe src="solution" height="260" link]
1010

11-
Requirements:
11+
Requisiti:
1212

13-
- The ball center should come exactly under the pointer on click (if possible without crossing the field edge).
14-
- CSS-animation is welcome.
15-
- The ball must not cross field boundaries.
16-
- When the page is scrolled, nothing should break.
13+
- Il centro della palla, al click, dovrà essere esattamente sotto il puntatore (possibilmente senza attraversare i bordi del campo).
14+
- Le animazioni CSS sono ben accette.
15+
- La palla non deve attraversare i confini del campo.
16+
- Allo scroll della pagina, lo script non si deve rompere.
1717

18-
Notes:
18+
Note:
1919

20-
- The code should also work with different ball and field sizes, not be bound to any fixed values.
21-
- Use properties `event.clientX/event.clientY` for click coordinates.
20+
- Il codice dovrebbe funzionare anche con differenti tipi di palle e campi, senza essere legato a nessun valore prefissato.
21+
- Usa le proprietà `event.clientX/event.clientY` per le coordinate del click.

2-ui/2-events/01-introduction-browser-events/05-sliding-menu/solution.md

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,45 @@
11

2-
# HTML/CSS
3-
First let's create HTML/CSS.
2+
# HTML e CSS
3+
Per prima cosa creiamo l'HTML ed il CSS.
44

5-
A menu is a standalone graphical component on the page, so it's better to put it into a single DOM element.
5+
Un menù è un componente grafico indipendente nella pagina, quindi è bene inserirlo all'interno di un singolo elemento genitore del DOM.
66

7-
A list of menu items can be laid out as a list `ul/li`.
7+
Una lista di elementi del menù può essere rappresentata da una lista di `ul/li`.
88

9-
Here's the example structure:
9+
Ecco la struttura d'esempio:
1010

1111
```html
1212
<div class="menu">
13-
<span class="title">Sweeties (click me)!</span>
13+
<span class="title">Dolciumi (cliccami)!</span>
1414
<ul>
15-
<li>Cake</li>
16-
<li>Donut</li>
17-
<li>Honey</li>
15+
<li>Torte</li>
16+
<li>Ciambelle</li>
17+
<li>Miele</li>
1818
</ul>
1919
</div>
2020
```
2121

22-
We use `<span>` for the title, because `<div>` has an implicit `display:block` on it, and it will occupy 100% of the horizontal width.
23-
24-
Like this:
22+
Usiamo `<span>` per il titolo perché `<div>`, avendo un `display:block` implicito, occuperebbe il 100% della larghezza disponibile.
2523

2624
```html autorun height=50
27-
<div style="border: solid red 1px" onclick="alert(1)">Sweeties (click me)!</div>
25+
<div style="border: solid red 1px" onclick="alert(1)">Dolciumi (cliccami)!</div>
2826
```
2927

30-
So if we set `onclick` on it, then it will catch clicks to the right of the text.
28+
Impostando un `onclick` su di esso, intercetterà i click alla destra del testo.
3129

32-
As `<span>` has an implicit `display: inline`, it occupies exactly enough place to fit all the text:
30+
Dato che `<span>` ha un `display: inline` implicito, occupa esattamente lo spazio necessario per la visualizzazione del testo:
3331

3432
```html autorun height=50
35-
<span style="border: solid red 1px" onclick="alert(1)">Sweeties (click me)!</span>
33+
<span style="border: solid red 1px" onclick="alert(1)">Dolciumi (cliccami)!</span>
3634
```
3735

38-
# Toggling the menu
36+
# Azionamento del menù
3937

40-
Toggling the menu should change the arrow and show/hide the menu list.
38+
L'azionamento del menù dovrebbe cambiare la direzione della freccia e mostrare/nascondere la lista degli elementi.
4139

42-
All these changes are perfectly handled by CSS. In JavaScript we should label the current state of the menu by adding/removing the class `.open`.
40+
Tutte queste modifiche sono gestite perfettamente dai CSS. In JavaScript dovremmo solamente etichettare lo stato corrente del menù aggiungendo/rimuovendo la classe `.open`.
4341

44-
Without it, the menu will be closed:
42+
Senza di essa, il menù risulterebbe chiuso:
4543

4644
```css
4745
.menu ul {
@@ -58,7 +56,7 @@ Without it, the menu will be closed:
5856
}
5957
```
6058

61-
...And with `.open` the arrow changes and the list shows up:
59+
...invece con `.open`, la frecca si modifica e la lista si apre:
6260

6361
```css
6462
.menu.open .title::before {

2-ui/2-events/01-introduction-browser-events/05-sliding-menu/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-
# Create a sliding menu
5+
# Create un menù a scorrimento
66

7-
Create a menu that opens/collapses on click:
7+
Create un menù che si apra e chiuda al click:
88

99
[iframe border=1 height=100 src="solution"]
1010

11-
P.S. HTML/CSS of the source document is to be modified.
11+
P.S.: HTML e CSS del codice della pagina andranno modificati.

2-ui/2-events/01-introduction-browser-events/06-hide-message/solution.md

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

2-
To add the button we can use either `position:absolute` (and make the pane `position:relative`) or `float:right`. The `float:right` has the benefit that the button never overlaps the text, but `position:absolute` gives more freedom. So the choice is yours.
2+
Per aggiungere un pulsante possiamo usare sia `position:absolute` (e quindi il suo contenitore dovrà avere `position:relative`) oppure `float:right`. `float:right` ha il vantaggio di non fare sovrapporre il pulsante al testo, `position:absolute` invece ci dà un po' più di libertà. La scelta è tua.
33

4-
Then for each pane the code can be like:
4+
Quindi per ogni contenitore il codice potrebbe essere come questo:
55
```js
66
pane.insertAdjacentHTML("afterbegin", '<button class="remove-button">[x]</button>');
77
```
88

9-
Then the `<button>` becomes `pane.firstChild`, so we can add a handler to it like this:
9+
Il `<button>` diventa `pane.firstChild`, e gli possiamo aggiungere un gestore come questo:
1010

1111
```js
1212
pane.firstChild.onclick = () => pane.remove();

0 commit comments

Comments
 (0)