You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit fde9b08
Browse filesBrowse the repository at this point in the historyBrowse files
The first handler triggers, because it's not removed by `removeEventListener`. To remove the handler we need to pass exactly the function that was assigned. And in the code a new function is passed, that looks the same, but is still another function.
3
+
Перший обробник спрацює, тому що він не був вилучений методом `removeEventListener`. Щоб видалити обробник, необхідно передати саме ту функцію, яка була призначена як обробник. Попри те, що код ідентичний, в `removeEventListener` передається нова, інша функція.
4
4
5
-
To remove a function object, we need to store a reference to it, like this:
5
+
Щоб видалити функцію-обробник, потрібно десь зберегти посилання на неї, наприклад:
First we need to choose a method of positioning the ball.
2
+
Спочатку ми маємо вибрати метод позиціювання м’яча.
3
3
4
-
We can't use `position:fixed`for it, because scrolling the page would move the ball from the field.
4
+
Ми не можемо використати `position:fixed`для цього, оскільки прокручування сторінки переміщатиме м’яч поля.
5
5
6
-
So we should use `position:absolute`and, to make the positioning really solid, make `field`itself positioned.
6
+
Правильніше використовувати `position:absolute`і, щоб зробити позиціювання справді надійним, зробимо саме поле `field`позиціонованим.
7
7
8
-
Then the ball will be positioned relatively to the field:
8
+
Тоді м’яч буде позиціонований щодо поля:
9
9
10
10
```css
11
11
#field {
@@ -16,36 +16,36 @@ Then the ball will be positioned relatively to the field:
16
16
17
17
#ball {
18
18
position: absolute;
19
-
left: 0; /*relative to the closest positioned ancestor (field) */
19
+
left: 0; /*по відношенню до найближчого розташованого предка (field) */
20
20
top: 0;
21
-
transition: 1sall; /* CSS animation for left/top makes the ball fly*/
21
+
transition: 1sall; /* CSS-анімація для значень left/top робить пересування м’яча плавним*/
22
22
}
23
23
```
24
24
25
-
Next we need to assign the correct `ball.style.left/top`. They contain field-relative coordinates now.
25
+
Далі ми маємо призначити коректні значення `ball.style.left/top`. Зараз вони містять координати щодо поля.
26
26
27
-
Here's the picture:
27
+
Як на зображенні:
28
28
29
29

30
30
31
-
We have`event.clientX/clientY` -- window-relative coordinates of the click.
31
+
Ми маємо`event.clientX/clientY` -- координати натискання мишки щодо вікна браузера.
32
32
33
-
To get field-relative`left`coordinate of the click, we can substract the field left edge and the border width:
33
+
Щоб отримати значення`left`для м’яча після натискання мишки щодо поля, ми повинні від координати натискання мишки відняти координату лівого краю поля та ширину межі:
34
34
35
35
```js
36
36
let left =event.clientX-fieldCoords.left-field.clientLeft;
37
37
```
38
38
39
-
Normally,`ball.style.left`means the "left edge of the element" (the ball). So if we assign that`left`, then the ball edge, not center, would be under the mouse cursor.
39
+
Значення`ball.style.left`означає «лівий край елемента» (м’яча). І якщо ми призначимо такий`left` для м’яча, то його ліва межа, а не центр, буде під курсором миші.
40
40
41
-
We need to move the ball half-width left and half-height up to make it center.
41
+
Нам потрібно зрушити м'яч на половину його висоти вгору та половину його ширини вліво, щоб центр м’яча точно збігався з точкою натискання мишки.
42
42
43
-
So the final `left`would be:
43
+
У результаті значення для `left`буде таким:
44
44
45
45
```js
46
46
let left =event.clientX-fieldCoords.left-field.clientLeft-ball.offsetWidth/2;
47
47
```
48
48
49
-
The vertical coordinate is calculated using the same logic.
49
+
Вертикальна координата обчислюватиметься за тією ж логікою.
50
50
51
-
Please note that the ball width/height must be known at the time we access `ball.offsetWidth`. Should be specified in HTML or CSS.
51
+
Слід пам’ятати, що ширина та висота м’яча має бути відома в той момент, коли ми отримуємо значення `ball.offsetWidth`. Це значення може бути задано в HTML або CSS.
0 commit comments