|
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`). |
2 | 2 |
|
3 |
| -The coordinates start from the inner left-upper corner of the field: |
| 3 | +Координати починаються з внутрішнього лівого верхнього кута поля: |
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 | +Ширина/висота внутрішнього поля `clientWidth/clientHeight`. Отже, центр поля має координати `(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 | +...Але якщо ми встановимо `ball.style.left/top` до таких значень, то в центрі буде не м'яч в цілому, а його лівий верхній кут: |
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 | +Ось як це виглядає: |
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 | +Щоб вирівняти центр м'яча з центром поля, ми повинні перемістити м'яч на половину його ширини вліво і на половину його висоти вгору: |
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 | +Тепер м'яч нарешті відцентрований. |
28 | 28 |
|
29 | 29 | ````warn header="Attention: the pitfall!"
|
30 | 30 |
|
31 |
| -The code won't work reliably while `<img>` has no width/height: |
| 31 | +Код не працюватиме надійно, поки `<img>` не має ширини/висоти: |
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 | +Коли браузер не знає ширини/висоти зображення (з атрибутів тегів або CSS), він вважає, що вони дорівнюють `0` поки не закінчиться завантаження зображення. |
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 | +Отже, значення `ball.offsetWidth` буде `0` поки не завантажиться зображення. Це призводить до неправильних координат у коді вище. |
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 | +Після першого завантаження браузер зазвичай кешує зображення, і при перезавантаженні воно відразу матиме розмір. Але при першому завантаженні значення `ball.offsetWidth` дорівнює `0`. |
43 | 43 |
|
44 |
| -We should fix that by adding `width/height` to `<img>`: |
| 44 | +Ми повинні це виправити, додавши `width/height` до `<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 | +...Або вказати розмір у CSS: |
51 | 51 |
|
52 | 52 | ```css
|
53 | 53 | #ball {
|
|
0 commit comments