Skip to content

Commit 42675cf

Browse files
committed
Update 2-ui\1-document\09-size-and-scroll\tasks
1 parent 65c5f95 commit 42675cf

File tree

5 files changed

+26
-26
lines changed

5 files changed

+26
-26
lines changed

2-ui/1-document/09-size-and-scroll/4-put-ball-in-center/ball-half/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
let ball = document.getElementById('ball')
3030
let field = document.getElementById('field')
3131

32-
// ball.offsetWidth=0 before image loaded!
33-
// to fix: set width
32+
// prima che l'immagine sia caricata ball.offsetWidth=0!
33+
// per correggere: imposta le dimensioni
3434
ball.style.left = Math.round(field.clientWidth / 2) + 'px'
3535
ball.style.top = Math.round(field.clientHeight / 2) + 'px'
3636
</script>

2-ui/1-document/09-size-and-scroll/4-put-ball-in-center/solution.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,53 @@
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`).
22

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:
44

55
![](field.svg)
66

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)`.
88

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
1010

1111
```js
1212
ball.style.left = Math.round(field.clientWidth / 2) + 'px';
1313
ball.style.top = Math.round(field.clientHeight / 2) + 'px';
1414
```
1515

16-
Here's how it looks:
16+
Ecco cosa otterremmo:
1717

1818
[iframe height=180 src="ball-half"]
1919

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:
2121

2222
```js
2323
ball.style.left = Math.round(field.clientWidth / 2 - ball.offsetWidth / 2) + 'px';
2424
ball.style.top = Math.round(field.clientHeight / 2 - ball.offsetHeight / 2) + 'px';
2525
```
2626

27-
Now the ball is finally centered.
27+
Adesso la palla è finalmente centrata.
2828

29-
````warn header="Attention: the pitfall!"
29+
````warn header="Attenzione: c'è una difficoltà imprevista!"
3030
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:
3232
3333
```html
3434
<img src="ball.png" id="ball">
3535
```
3636
````
3737

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.
3939

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.
4141

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`.
4343

44-
We should fix that by adding `width/height` to `<img>`:
44+
Dovremmo correggere aggiungendo `width/height` a `<img>`:
4545

4646
```html
4747
<img src="ball.png" *!*width="40" height="40"*/!* id="ball">
4848
```
4949

50-
...Or provide the size in CSS:
50+
...o fornire le dimensioni nei CSS:
5151

5252
```css
5353
#ball {

2-ui/1-document/09-size-and-scroll/4-put-ball-in-center/solution.view/index.html

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

2727

2828
<script>
29-
// ball.offsetWidth=0 before image loaded!
30-
// to fix: set width
29+
// prima che l'immagine sia caricata ball.offsetWidth=0!
30+
// per correggere: imposta le dimensioni
3131
ball.style.left = Math.round(field.clientWidth / 2 - ball.offsetWidth / 2) + 'px'
3232
ball.style.top = Math.round(field.clientHeight / 2 - ball.offsetHeight / 2) + 'px'
3333
</script>

2-ui/1-document/09-size-and-scroll/4-put-ball-in-center/task.md

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

33
---
44

5-
# Place the ball in the field center
5+
# Posiziona la palla al centro del campo
66

7-
Here's how the source document looks:
7+
Ecco come si presenta il documento di partenza:
88

99
[iframe src="source" edit link height=180]
1010

11-
What are coordinates of the field center?
11+
Quali sono le coordinate del centro del campo?
1212

13-
Calculate them and use to place the ball into the center of the green field:
13+
Calcolale e usale per posizionare la palla al centro del campo verde:
1414

1515
[iframe src="solution" height=180]
1616

17-
- The element should be moved by JavaScript, not CSS.
18-
- The code should work with any ball size (`10`, `20`, `30` pixels) and any field size, not be bound to the given values.
17+
- L'elemento dovrebbe essere spostato con JavaScript, non con i CSS.
18+
- Il codice dovrebbe funzionare anche con una dimensione della palla differente (`10`, `20`, `30` pixel) e qualunque dimensione del campo: non dovrebbe essere legato a valori noti.
1919

20-
P.S. Sure, centering could be done with CSS, but here we want exactly JavaScript. Further we'll meet other topics and more complex situations when JavaScript must be used. Here we do a "warm-up".
20+
P.S. Certamente, il posizionamento al centro potrebbe essere ottenuto con i CSS, ma qui vi chiediamo di farlo proprio con JavaScript. Più avanti incotreremo altri casi e situzioni più complesse in cui JavaScript è l'unica alternativa. Ora ci stiamo solo "scaldando".

2-ui/1-document/09-size-and-scroll/cssWidthScroll.view/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
text text text text text text text text text text text text text text text text text text text text text text text text text text
88
</div>
99

10-
The element has <code>style="width:300px"</code>
10+
L'elemento ha <code>style="width:300px"</code>
1111
<br>
1212

1313
<button onclick="alert( getComputedStyle(elem).width )">alert( getComputedStyle(elem).width )</button>

0 commit comments

Comments
 (0)