Skip to content

Commit 65025fd

Browse files
authored
Merge pull request #187 from HachemiH/master
Element size and scrolling
2 parents dd06aff + 8f06915 commit 65025fd

File tree

9 files changed

+139
-139
lines changed

9 files changed

+139
-139
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 solution est :
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+
En d'autres termes : (pleine hauteur) moins (partie supérieure déroulée) moins (partie visible) -- c'est exactement la partie inférieure déroulée.

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+
# Quel est le défilement à partir du bas ?
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 propriété `elem.scrollTop` est la taille de la partie déroulante à partir du haut. Comment obtenir la taille du défilement inférieur (appelons-le `scrollBottom`) ?
88

9-
Write the code that works for an arbitrary `elem`.
9+
Écrivez le code qui fonctionne pour un `element` arbitraire.
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. Veuillez vérifier votre code: s'il n'y a pas de défilement ou que l'élément est entièrement défilé vers le bas, alors il devrait retourner `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+
Pour obtenir la largeur de la barre de défilement, nous pouvons créer un élément avec le défilement, mais sans bordures ni paddings.
22

3-
Then the difference between its full width `offsetWidth` and the inner content area width `clientWidth` will be exactly the scrollbar:
3+
Ensuite, la différence entre sa largeur totale `offsetWidth` et la largeur de la zone de contenu interne `clientWidth` sera exactement la barre de défilement :
44

55
```js run
6-
// create a div with the scroll
6+
// créer une div avec le défilement
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+
// doit le mettre dans le document, sinon les tailles seront 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+
# Quelle est la largeur de la barre de défilement ?
66

7-
Write the code that returns the width of a standard scrollbar.
7+
Écrivez le code qui renvoie la largeur d'une barre de défilement 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+
Pour Windows, il varie généralement entre `12px` et `20px`. Si le navigateur ne lui réserve pas d'espace (la barre de défilement est à moitié translucide sur le texte, cela arrive également), alors il peut s'agir de `0px`.
1010

11-
P.S. The code should work for any HTML document, do not depend on its content.
11+
P.S. Le code devrait fonctionner pour tout document HTML, ne dépend pas de son contenu.

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 balle a une `position:absolue`. Cela signifie que ses coordonnées `gauche/haut` sont mesurées à partir de l'élément positionné le plus proche, c'est-à-dire `#field` (car il a `position:relative`).
22

3-
The coordinates start from the inner left-upper corner of the field:
3+
Les coordonnées commencent à partir du coin supérieur gauche intérieur du champ :
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+
La largeur/hauteur du champ intérieur est `clientWidth/clientHeight`. Le centre de terrain a donc les coordonnées `(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+
...Mais si nous définissons `ball.style.left/top` à de telles valeurs, alors pas la balle dans son ensemble, mais le bord supérieur gauche de la balle serait au centre :
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+
Voici à quoi ça ressemble :
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+
Pour aligner le centre de la balle avec le centre du terrain, nous devons déplacer la balle sur la moitié de sa largeur à gauche et sur la moitié de sa hauteur vers le haut :
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+
Maintenant, la balle est enfin centrée.
2828

29-
````warn header="Attention: the pitfall!"
29+
````warn header="Attention : l'écueil !"
3030
31-
The code won't work reliably while `<img>` has no width/height:
31+
Le code ne fonctionnera pas de manière fiable tant que `<img>` n'a pas de largeur/hauteur :
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+
Lorsque le navigateur ne connaît pas la largeur/hauteur d'une image (à partir des attributs de balise ou CSS), il suppose qu'ils sont égaux à `0` jusqu'à ce que le chargement de l'image soit terminé.
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+
Ainsi, la valeur de `ball.offsetWidth` sera `0` jusqu'à ce que l'image se charge. Cela conduit à de mauvaises coordonnées dans le code ci-dessus.
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+
Après le premier chargement, le navigateur met généralement l'image en cache et lors des rechargements, elle aura immédiatement la taille. Mais lors du premier chargement, la valeur de `ball.offsetWidth` est `0`.
4343

44-
We should fix that by adding `width/height` to `<img>`:
44+
Nous devons corriger cela en ajoutant `width/height` à `<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+
...Ou indiquez la taille en CSS :
5151

5252
```css
5353
#ball {

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+
# Placer la balle au centre du terrain
66

7-
Here's how the source document looks:
7+
Voici à quoi ressemble le document source :
88

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

11-
What are coordinates of the field center?
11+
Quelles sont les coordonnées du centre de terrain ?
1212

13-
Calculate them and use to place the ball into the center of the green field:
13+
Calculez et utilisez-les pour placer la balle au centre du champ vert :
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'élément doit être déplacé par JavaScript, pas CSS.
18+
- Le code doit fonctionner avec n'importe quelle taille de boule (`10`, `20`, `30` pixels) et n'importe quelle taille de champ, ne pas être lié aux valeurs données.
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. Bien sûr, le centrage pourrait être effectué avec CSS, mais ici, nous voulons exactement JavaScript. De plus, nous rencontrerons d'autres sujets et des situations plus complexes lorsque JavaScript doit être utilisé. Ici, nous faisons un "échauffement".
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
Differences:
1+
Différences :
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` est numérique, tandis que `getComputedStyle(elem).width` renvoie une chaîne de caractères avec `px` à la fin.
4+
2. `getComputedStyle` peut renvoyer une largeur non numérique comme `"auto"` pour un élément inline.
5+
3. `clientWidth` est la zone de contenu interne de l'élément plus les paddings, tandis que la largeur CSS (avec le `box-sizing` standard ) est la zone de contenu interne *sans les paddings*.
6+
4. S'il y a une barre de défilement et que le navigateur lui réserve de l'espace, certains navigateurs soustraient cet espace de la largeur CSS (car il n'est plus disponible pour le contenu), et d'autres non. La propriété `clientWidth` est toujours la même : la taille de la barre de défilement est soustraite si elle est réservée.

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+
# La différence: largeur CSS vs clientWidth
66

7-
What's the difference between `getComputedStyle(elem).width` and `elem.clientWidth`?
7+
Quelle est la différence entre `getComputedStyle(elem).width` et `elem.clientWidth` ?
88

9-
Give at least 3 differences. The more the better.
9+
Donnez au moins 3 différences. Plus c'est mieux.

0 commit comments

Comments
 (0)