|
1 | | -The ball has `position:absolute`. It means that its `left/top` coordinates are measured from the nearest positioned element, that is `#field` (because it has `position:relative`). |
| 1 | +La palla ha `position:absolute`. Ciò significa che le coordinate `left/top` sono relative all'elemento posizionato più prossimo, cioè `#field` (perché ha `position:relative`). |
2 | 2 |
|
3 | | -The coordinates start from the inner left-upper corner of the field: |
| 3 | +Le coordinate sono a partire dall'angolo interno superiore sinistro del campo: |
4 | 4 |
|
5 | 5 |  |
6 | 6 |
|
7 | | -The inner field width/height is `clientWidth/clientHeight`. So the field center has coordinates `(clientWidth/2, clientHeight/2)`. |
| 7 | +Le dimensioni interne del campo si calcolano con `clientWidth/clientHeight`. I valori delle coordinate del centro del campo quindi si ottengono con `(clientWidth/2, clientHeight/2)`. |
8 | 8 |
|
9 | | -...But if we set `ball.style.left/top` to such values, then not the ball as a whole, but the left-upper edge of the ball would be in the center: |
| 9 | +...Ma se impostiamo tali valori per `ball.style.left/top`, allora si troverebbe al centro non la palla ma il suo bordo superiore sinistro |
10 | 10 |
|
11 | 11 | ```js |
12 | 12 | ball.style.left = Math.round(field.clientWidth / 2) + 'px'; |
13 | 13 | ball.style.top = Math.round(field.clientHeight / 2) + 'px'; |
14 | 14 | ``` |
15 | 15 |
|
16 | | -Here's how it looks: |
| 16 | +Ecco cosa otterremmo: |
17 | 17 |
|
18 | 18 | [iframe height=180 src="ball-half"] |
19 | 19 |
|
20 | | -To align the ball center with the center of the field, we should move the ball to the half of its width to the left and to the half of its height to the top: |
| 20 | +Per allineare il centro della palla con il centro del campo, dovremmo spostare la palla alla metà della sua larghezza a sinistra ed alla metà della sua altezza verso l'alto: |
21 | 21 |
|
22 | 22 | ```js |
23 | 23 | ball.style.left = Math.round(field.clientWidth / 2 - ball.offsetWidth / 2) + 'px'; |
24 | 24 | ball.style.top = Math.round(field.clientHeight / 2 - ball.offsetHeight / 2) + 'px'; |
25 | 25 | ``` |
26 | 26 |
|
27 | | -Now the ball is finally centered. |
| 27 | +Adesso la palla è finalmente centrata. |
28 | 28 |
|
29 | | -````warn header="Attention: the pitfall!" |
| 29 | +````warn header="Attenzione: c'è una difficoltà imprevista!" |
30 | 30 |
|
31 | | -The code won't work reliably while `<img>` has no width/height: |
| 31 | +Il codice non funzionerà in modo affidabile finché `<img>` non avrà larghezza ed altezza definite: |
32 | 32 |
|
33 | 33 | ```html |
34 | 34 | <img src="ball.png" id="ball"> |
35 | 35 | ``` |
36 | 36 | ```` |
37 | 37 |
|
38 | | -When the browser does not know the width/height of an image (from tag attributes or CSS), then it assumes them to equal `0` until the image finishes loading. |
| 38 | +Quando il browser non conosce le dimensioni di un'immagine (dagli attributi del tag o dai CSS), allora assume che siano pari a `0` finché l'immagine non completa il caricamento. |
39 | 39 |
|
40 | | -So the value of `ball.offsetWidth` will be `0` until the image loads. That leads to wrong coordinates in the code above. |
| 40 | +Pertanto il valore di `ball.offsetWidth` sarà `0` fino al momento in cui l'immagine non viene caricata. Questo causerà coordinate errate nel codice sopra. |
41 | 41 |
|
42 | | -After the first load, the browser usually caches the image, and on reloads it will have the size immediately. But on the first load the value of `ball.offsetWidth` is `0`. |
| 42 | +Dopo il primo caricamento, il browser solitamente mette in cache l'immagine, e ne ricorderà subito le dimensioni se la dovesse ricaricare. Al primo caricamento, tuttavia, il valore di `ball.offsetWidth` è `0`. |
43 | 43 |
|
44 | | -We should fix that by adding `width/height` to `<img>`: |
| 44 | +Dovremmo correggere aggiungendo `width/height` a `<img>`: |
45 | 45 |
|
46 | 46 | ```html |
47 | 47 | <img src="ball.png" *!*width="40" height="40"*/!* id="ball"> |
48 | 48 | ``` |
49 | 49 |
|
50 | | -...Or provide the size in CSS: |
| 50 | +...o fornire le dimensioni nei CSS: |
51 | 51 |
|
52 | 52 | ```css |
53 | 53 | #ball { |
|
0 commit comments