Skip to content

Commit c81265a

Browse files
authored
Merge pull request #407 from 80001/patch-7
Update solution.md
2 parents cc08105 + c59ca24 commit c81265a

File tree

1 file changed

+13
-13
lines changed
  • 2-ui/1-document/09-size-and-scroll/4-put-ball-in-center

1 file changed

+13
-13
lines changed

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

Lines changed: 13 additions & 13 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+
М'яч має `position:absolute`. Це означає, що його `left/top` координати вимірюються від найближчого розташованого елемента(батька), тобто `#field` (тому що він має `position:relative`).
22

3-
The coordinates start from the inner left-upper corner of the field:
3+
Координати починаються з внутрішнього лівого верхнього кута поля:
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+
Ширина/висота внутрішнього поля `clientWidth/clientHeight`. Отже, центр поля має координати `(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+
...Але якщо ми встановимо `ball.style.left/top` до таких значень, то в центрі буде не м'яч в цілому, а його лівий верхній кут:
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+
Ось як це виглядає:
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+
Щоб вирівняти центр м'яча з центром поля, ми повинні перемістити м'яч на половину його ширини вліво і на половину його висоти вгору:
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+
Тепер м'яч нарешті відцентрований.
2828

2929
````warn header="Attention: the pitfall!"
3030
31-
The code won't work reliably while `<img>` has no width/height:
31+
Код не працюватиме надійно, поки `<img>` не має ширини/висоти:
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+
Коли браузер не знає ширини/висоти зображення (з атрибутів тегів або CSS), він вважає, що вони дорівнюють `0` поки не закінчиться завантаження зображення.
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+
Отже, значення `ball.offsetWidth` буде `0` поки не завантажиться зображення. Це призводить до неправильних координат у коді вище.
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+
Після першого завантаження браузер зазвичай кешує зображення, і при перезавантаженні воно відразу матиме розмір. Але при першому завантаженні значення `ball.offsetWidth` дорівнює `0`.
4343

44-
We should fix that by adding `width/height` to `<img>`:
44+
Ми повинні це виправити, додавши `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+
...Або вказати розмір у CSS:
5151

5252
```css
5353
#ball {

0 commit comments

Comments
 (0)