Skip to content

Commit 7db55fd

Browse files
authored
Merge pull request #279 from marcellosurdi/article/size-and-scroll
Element size and scrolling
2 parents 9b5fc32 + 5657400 commit 7db55fd

File tree

13 files changed

+147
-148
lines changed

13 files changed

+147
-148
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
The solution is:
1+
La soluzione è:
22

33
```js
44
let scrollBottom = elem.scrollHeight - elem.scrollTop - elem.clientHeight;
55
```
66

7-
In other words: (full height) minus (scrolled out top part) minus (visible part) -- that's exactly the scrolled out bottom part.
7+
In altre parole: (altezza totale) meno (misura dello scorrimento dall'alto) meno (altezza dell'area di scorrimento visibile). Il risultato è esattamente la misura della parte inferiore che resta da scorrere.

2-ui/1-document/09-size-and-scroll/1-get-scroll-height-bottom/task.md

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

33
---
44

5-
# What's the scroll from the bottom?
5+
# Qual è la misura dello scorrimento verso il basso?
66

7-
The `elem.scrollTop` property is the size of the scrolled out part from the top. How to get the size of the bottom scroll (let's call it `scrollBottom`)?
7+
La proprietà `elem.scrollTop` è la misura della parte superiore di un elemento fuori dall'area di scorrimento. Come ottenere la misura della parte inferiore (chiamiamola `scrollBottom`)?
88

9-
Write the code that works for an arbitrary `elem`.
9+
Scrivete il codice che funzioni per un elemento arbitrario `elem`.
1010

11-
P.S. Please check your code: if there's no scroll or the element is fully scrolled down, then it should return `0`.
11+
P.S. Verificate il vostro codice: se non c'è scorrimento o è stato effettuato tutto lo scorrimento verso il basso, allora dovrebbe restituire `0`.

2-ui/1-document/09-size-and-scroll/2-scrollbar-width/solution.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
To get the scrollbar width, we can create an element with the scroll, but without borders and paddings.
1+
Per ricavare la larghezza della barra di scorrimento, possiamo creare un elemento con scorrimento ma senza bordi e padding.
22

3-
Then the difference between its full width `offsetWidth` and the inner content area width `clientWidth` will be exactly the scrollbar:
3+
In quel caso la sottrazione tra la larghezza totale `offsetWidth` e la larghezza dell'area interna del contenuto `clientWidth` equivarrà esattamente alla larghezza della barra di scorrimento:
44

55
```js run
6-
// create a div with the scroll
6+
// creiamo un div con scorrimento
77
let div = document.createElement('div');
88

99
div.style.overflowY = 'scroll';
1010
div.style.width = '50px';
1111
div.style.height = '50px';
1212

13-
// must put it in the document, otherwise sizes will be 0
13+
// dobbiamo inserirlo nel flusso del documento, altrimenti le dimensioni saranno pari a 0
1414
document.body.append(div);
1515
let scrollWidth = div.offsetWidth - div.clientWidth;
1616

2-ui/1-document/09-size-and-scroll/2-scrollbar-width/task.md

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

33
---
44

5-
# What is the scrollbar width?
5+
# Qual è la larghezza della barra di scorrimento?
66

7-
Write the code that returns the width of a standard scrollbar.
7+
Scrivete il codice che restituisca la larghezza di una barra di scorrimento standard.
88

9-
For Windows it usually varies between `12px` and `20px`. If the browser doesn't reserve any space for it (the scrollbar is half-translucent over the text, also happens), then it may be `0px`.
9+
Per Windows solitamente varia tra `12px` e `20px`. Se il browser non le riserva alcuno spazio (capita che la barra di scorrimento appaia semi-opaca sopra il testo), allora può essere `0px`.
1010

11-
P.S. The code should work for any HTML document, do not depend on its content.
11+
P.S. Il codice dovrebbe funzionare per ogni documento HTML indipendentemente dal contenuto.

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 incontreremo altri casi e situazioni più complesse in cui JavaScript è l'unica alternativa. Ora ci stiamo solo "scaldando".
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
Differences:
1+
Differenze:
22

3-
1. `clientWidth` is numeric, while `getComputedStyle(elem).width` returns a string with `px` at the end.
4-
2. `getComputedStyle` may return non-numeric width like `"auto"` for an inline element.
5-
3. `clientWidth` is the inner content area of the element plus paddings, while CSS width (with standard `box-sizing`) is the inner content area *without paddings*.
6-
4. If there's a scrollbar and the browser reserves the space for it, some browser substract that space from CSS width (cause it's not available for content any more), and some do not. The `clientWidth` property is always the same: scrollbar size is substracted if reserved.
3+
1. `clientWidth` è un valore numerico, `getComputedStyle(elem).width` invece restituisce una stringa con `px` alla fine.
4+
2. `getComputedStyle` può restituire una larghezza non numerica come `"auto"` per un elemento inline.
5+
3. `clientWidth` è l'area del contenuto interna di un elemento più il padding, mentre la proprietà width dei CSS (con il valore predefinito di `box-sizing`) è l'area del contenuto interna *senza il padding*.
6+
4. Se c'è una barra di scorrimento ed il browser le riserva uno spazio, alcuni browser sottraggono quello spazio alla larghezza impostata tramite CSS (perché non è più disponibile per i contenuti), e altri invece no. La proprietà `clientWidth` è sempre la stessa: se la barra di scorrimento ha uno spazio riservato viene sottratto all'area del contenuto.

2-ui/1-document/09-size-and-scroll/6-width-vs-clientwidth/task.md

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

33
---
44

5-
# The difference: CSS width versus clientWidth
5+
# Indica le differenze tra la proprietà width CSS e clientWidth
66

7-
What's the difference between `getComputedStyle(elem).width` and `elem.clientWidth`?
7+
Quali sono le differenze tra `getComputedStyle(elem).width` e `elem.clientWidth`?
88

9-
Give at least 3 differences. The more the better.
9+
Indica almeno 3 differenze. Più sono meglio è.

0 commit comments

Comments
 (0)