|
1 | | -# Moving the mouse: mouseover/out, mouseenter/leave |
| 1 | +# Переміщення миші: mouseover/out, mouseenter/leave |
2 | 2 |
|
3 | | -Let's dive into more details about events that happen when the mouse moves between elements. |
| 3 | +Давайте детальніше розглянемо події, які відбуваються, коли вказівник миші переміщається між елементами. |
4 | 4 |
|
5 | | -## Events mouseover/mouseout, relatedTarget |
| 5 | +## Події mouseover/mouseout, relatedTarget |
6 | 6 |
|
7 | | -The `mouseover` event occurs when a mouse pointer comes over an element, and `mouseout` -- when it leaves. |
| 7 | +Подія `mouseover` виникає, коли вказівник миші наводиться на елемент, а `mouseout` -- коли залишає його. |
8 | 8 |
|
9 | 9 |  |
10 | 10 |
|
11 | | -These events are special, because they have property `relatedTarget`. This property complements `target`. When a mouse leaves one element for another, one of them becomes `target`, and the other one - `relatedTarget`. |
| 11 | +Ці події особливі, оскільки мають властивість `relatedTarget`. Ця властивість доповнює `target`. Коли миша йде від одного елемента до іншого, один з них стає `target`, а інший -- `relatedTarget`. |
12 | 12 |
|
13 | | -For `mouseover`: |
| 13 | +Для `mouseover`: |
14 | 14 |
|
15 | | -- `event.target` -- is the element where the mouse came over. |
16 | | -- `event.relatedTarget` -- is the element from which the mouse came (`relatedTarget` -> `target`). |
| 15 | +- `event.target` -- це елемент, на який наведено вказівник миші. |
| 16 | +- `event.relatedTarget` -- це елемент, з якого прийшов вказіник (`relatedTarget` -> `target`). |
17 | 17 |
|
18 | | -For `mouseout` the reverse: |
| 18 | +Для `mouseout` навпаки: |
19 | 19 |
|
20 | | -- `event.target` -- is the element that the mouse left. |
21 | | -- `event.relatedTarget` -- is the new under-the-pointer element, that mouse left for (`target` -> `relatedTarget`). |
| 20 | +- `event.target` -- це елемент, який залишила миша. |
| 21 | +- `event.relatedTarget` -- це новий елемент під вказівником, на який перейшла миша (`target` -> `relatedTarget`). |
22 | 22 |
|
23 | 23 | ```online |
24 | | -In the example below each face and its features are separate elements. When you move the mouse, you can see mouse events in the text area. |
| 24 | +У наведеному нижче прикладі кожне обличчя та його риси є окремими елементами. Коли ви рухаєте мишею, події миші відображаються в текстовій області. |
25 | 25 |
|
26 | | -Each event has the information about both `target` and `relatedTarget`: |
| 26 | +Кожна подія містить інформацію як про `target`, так і про `relatedTarget`: |
27 | 27 |
|
28 | 28 | [codetabs src="mouseoverout" height=280] |
29 | 29 | ``` |
30 | 30 |
|
31 | | -```warn header="`relatedTarget` can be `null`" |
32 | | -The `relatedTarget` property can be `null`. |
| 31 | +```warn header="`relatedTarget` може бути `null`" |
| 32 | +Властивість `relatedTarget` може мати значення `null`. |
33 | 33 |
|
34 | | -That's normal and just means that the mouse came not from another element, but from out of the window. Or that it left the window. |
| 34 | +Це нормально і просто означає, що вказівник миші прийшов не з іншого елемента, а десь з вікна. Або навпаки, що вказівник вийшов за межі вікна браузера. |
35 | 35 |
|
36 | | -We should keep that possibility in mind when using `event.relatedTarget` in our code. If we access `event.relatedTarget.tagName`, then there will be an error. |
| 36 | +Нам варто пам'ятати про цю можливість, використовуючи `event.relatedTarget` в коді. Бо якщо спробувати отримати доступ до `event.relatedTarget.tagName`, то виникне помилка. |
37 | 37 | ``` |
38 | 38 |
|
39 | | -## Skipping elements |
| 39 | +## Пропуск елементів |
40 | 40 |
|
41 | | -The `mousemove` event triggers when the mouse moves. But that doesn't mean that every pixel leads to an event. |
| 41 | +Подія `mousemove` запускається, коли миша рухається. Але це не означає, що кожен навіть найменший рух веде до окремої події. |
42 | 42 |
|
43 | | -The browser checks the mouse position from time to time. And if it notices changes then triggers the events. |
| 43 | +Час від часу браузер перевіряє положення миші. І якщо він помічає зміни, то запускає події. |
44 | 44 |
|
45 | | -That means that if the visitor is moving the mouse very fast then some DOM-elements may be skipped: |
| 45 | +Це означає, що якщо користувач рухає мишею дуже швидко, деякі DOM-елементи можуть бути пропущені: |
46 | 46 |
|
47 | 47 |  |
48 | 48 |
|
49 | | -If the mouse moves very fast from `#FROM` to `#TO` elements as painted above, then intermediate `<div>` elements (or some of them) may be skipped. The `mouseout` event may trigger on `#FROM` and then immediately `mouseover` on `#TO`. |
| 49 | +Якщо миша дуже швидко рухається від елементів `#FROM` до `#TO`, як зазначено вище, то проміжні елементи `<div>` (або деякі з них) можуть бути пропущені. Подія `mouseout` може бути ініційована на `#FROM`, а потім одразу `mouseover`на `#TO`. |
50 | 50 |
|
51 | | -That's good for performance, because there may be many intermediate elements. We don't really want to process in and out of each one. |
| 51 | +Це добре для продуктивності, бо може бути багато проміжних елементів. Ми насправді не хочемо обробляти кожен із них. |
52 | 52 |
|
53 | | -On the other hand, we should keep in mind that the mouse pointer doesn't "visit" all elements along the way. It can "jump". |
| 53 | +З іншого боку, ми повинні мати на увазі, що вказівник миші не "відвідує" всі елементи на шляху і може "стрибати". |
54 | 54 |
|
55 | | -In particular, it's possible that the pointer jumps right inside the middle of the page from out of the window. In that case `relatedTarget` is `null`, because it came from "nowhere": |
| 55 | +Зокрема, можливо, що вказівник стрибне прямо всередину сторінки з поза меж вікна. У цьому випадку `relatedTarget` має значення `null`, тому що він прийшов "нізвідки": |
56 | 56 |
|
57 | 57 |  |
58 | 58 |
|
59 | 59 | ```online |
60 | | -You can check it out "live" on a teststand below. |
| 60 | +Ви можете перевірити це на тестовому стенді нижче. |
61 | 61 |
|
62 | | -Its HTML has two nested elements: the `<div id="child">` is inside the `<div id="parent">`. If you move the mouse fast over them, then maybe only the child div triggers events, or maybe the parent one, or maybe there will be no events at all. |
| 62 | +Його HTML має два вкладені елементи: `<div id="child">` знаходиться всередині `<div id="parent">`. Якщо ви швидко наведете на них мишу, то, можливо, лише дочірній div ініціює події, або батьківський, або навіть подій не буде взагалі. |
63 | 63 |
|
64 | | -Also move the pointer into the child `div`, and then move it out quickly down through the parent one. If the movement is fast enough, then the parent element is ignored. The mouse will cross the parent element without noticing it. |
| 64 | +Також перемістіть вказівник у дочірній `div`, а потім швидко перемістіть його вниз через батьківський. Якщо рух досить швидкий, то батьківський елемент ігнорується. Миша перетне батьківський елемент, не помітивши цього. |
65 | 65 |
|
66 | 66 | [codetabs height=360 src="mouseoverout-fast"] |
67 | 67 | ``` |
68 | 68 |
|
69 | | -```smart header="If `mouseover` triggered, there must be `mouseout`" |
70 | | -In case of fast mouse movements, intermediate elements may be ignored, but one thing we know for sure: if the pointer "officially" entered an element (`mouseover` event generated), then upon leaving it we always get `mouseout`. |
| 69 | +```smart header="If `Якщо спрацьовує `mouseover`, має бути `mouseout`" |
| 70 | +У разі швидких рухів миші проміжні елементи можуть ігноруватися, але одне ми знаємо напевно: якщо вказівник "офіційно" увійшов на елемент (генерується подія `mouseover`), то при виході з нього ми завжди отримуємо `mouseout`. |
71 | 71 | ``` |
72 | 72 |
|
73 | | -## Mouseout when leaving for a child |
| 73 | +## Mouseout при переході на дочірній елемент |
74 | 74 |
|
75 | | -An important feature of `mouseout` -- it triggers, when the pointer moves from an element to its descendant, e.g. from `#parent` to `#child` in this HTML: |
| 75 | +Важлива функція події `mouseout` -- вона запускається, коли вказівник переміщується від елемента до його нащадка, наприклад, від `#parent` до `#child` у HTML нижче: |
76 | 76 |
|
77 | 77 | ```html |
78 | 78 | <div id="parent"> |
79 | 79 | <div id="child">...</div> |
80 | 80 | </div> |
81 | 81 | ``` |
82 | 82 |
|
83 | | -If we're on `#parent` and then move the pointer deeper into `#child`, we get `mouseout` on `#parent`! |
| 83 | +Якщо ми знаходимося на `#parent`, а потім переміщуємо вказівник глибше в `#child`, ми отримуємо `mouseout` на `#parent`! |
84 | 84 |
|
85 | 85 |  |
86 | 86 |
|
87 | | -That may seem strange, but can be easily explained. |
| 87 | +Це може здатися дивним, але це легко пояснити. |
88 | 88 |
|
89 | | -**According to the browser logic, the mouse cursor may be only over a *single* element at any time -- the most nested one and top by z-index.** |
| 89 | +**Відповідно до логіки браузера, вказівник миші може бути лише над *одним* елементом у будь-який момент часу -- найбільш вкладеним і верхнім за z-індексом.** |
90 | 90 |
|
91 | | -So if it goes to another element (even a descendant), then it leaves the previous one. |
| 91 | +Отже, якщо він переходить до іншого елемента (навіть до нащадка), то він залишає попередній. |
92 | 92 |
|
93 | | -Please note another important detail of event processing. |
| 93 | +Зверніть увагу на ще одну важливу деталь обробки подій. |
94 | 94 |
|
95 | | -The `mouseover` event on a descendant bubbles up. So, if `#parent` has `mouseover` handler, it triggers: |
| 95 | +Подія `mouseover` на нащадку буде спливати. Отже, якщо `#parent` має обробник `mouseover`, він спрацює: |
96 | 96 |
|
97 | 97 |  |
98 | 98 |
|
99 | 99 | ```online |
100 | | -You can see that very well in the example below: `<div id="child">` is inside the `<div id="parent">`. There are `mouseover/out` handlers on `#parent` element that output event details. |
| 100 | +Ви можете це добре побачити в прикладі нижче: `<div id="child">` знаходиться всередині `<div id="parent">`. І обробники `mouseover/out` для елементу `#parent` виведуть деталі події. |
101 | 101 |
|
102 | 102 | If you move the mouse from `#parent` to `#child`, you see two events on `#parent`: |
103 | 103 | 1. `mouseout [target: parent]` (left the parent), then |
|
0 commit comments