Skip to content

Commit 6f51a2b

Browse files
committed
mouseover/out part 2
1 parent f9bce24 commit 6f51a2b

File tree

1 file changed

+25
-25
lines changed
  • 2-ui/3-event-details/3-mousemove-mouseover-mouseout-mouseenter-mouseleave

1 file changed

+25
-25
lines changed

2-ui/3-event-details/3-mousemove-mouseover-mouseout-mouseenter-mouseleave/article.md

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,67 +38,67 @@
3838
3939
## Частота запуска событий
4040
41-
The `mousemove` event triggers when the mouse moves. But that doesn't mean that every pixel leads to an event.
41+
Событие `mousemove` происходит при движении мыши. Однако, это не означает, что указанное событие генерируется при прохождении каждого пикселя.
4242
43-
The browser checks the mouse position from time to time. And if it notices changes then triggers the events.
43+
Браузер периодически проверяет позицию курсора и, заметив изменения, генерирует события `mousemove`.
4444
45-
That means that if the visitor is moving the mouse very fast then DOM-elements may be skipped:
45+
Это означает, что если пользователь двигает мышкой очень быстро, то некоторые DOM-элементы могут быть пропущены:
4646
4747
![](mouseover-mouseout-over-elems.png)
4848
49-
If the mouse moves very fast from `#FROM` to `#TO` elements as painted above, then intermediate `<div>` (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`.
5050
51-
In practice that's helpful, because if there may be many intermediate elements. We don't really want to process in and out of each one.
51+
На практике это даже может быть полезно, потому что если промежуточных элементов много, то на самом деле не хотелось бы обрабатывать события `mouseout` и `mouseover` для каждого из них.
5252
53-
On the other hand, we should keep in mind that we can't assume that the mouse slowly moves from one event to another. No, it can "jump".
53+
С другой стороны, не стоит рассчитывать, что курсор мыши будет медленно переходить от одного элемента к другому. Он может и "прыгать".
5454
55-
In particular it's possible that the cursor jumps right inside the middle of the page from out of the window. And `relatedTarget=null`, because it came from "nowhere":
55+
В частности, возможно, что курсор запрыгнет в середину страницы из-за пределов окна браузера, и в таком случае получится, что `relatedTarget=null`, так как курсор пришёл "из ниоткуда":
5656
5757
![](mouseover-mouseout-from-outside.png)
5858
5959
<div style="display:none">
60-
In case of a fast move, intermediate elements may trigger no events. But if the mouse enters the element (`mouseover`), when we're guaranteed to have `mouseout` when it leaves it.
60+
В случае быстрого прохождения курсором промежуточных элементов события вообще могут не генерироваться. Но если уж курсор пришёл на элемент (было сгенерировано `mouseover`), то гарантировано при выходе с того элемента будет запущено событие `mouseout`.
6161
</div>
6262
6363
```online
64-
Check it out "live" on a teststand below.
64+
Проверьте это в песочнице ниже.
6565
66-
The HTML is two nested `<div>` elements. If you move the mouse fast over them, then there may be no events at all, or maybe only the red div triggers events, or maybe the green one.
66+
Вёрстка состоит из двух элементов типа `<div>`, один из которых вложен в другой. Если быстро провести мышью над ними, то может не будет сгенерировано никаких событий вообще, а может события будут запущены только на красном элементе, а может только на зеленом.
6767
68-
Also try to move the pointer over the red `div`, and then move it out quickly down through the green one. If the movement is fast enough then the parent element is ignored.
68+
Также попробуйте поставить курсор на красный элемент, а затем очень быстро сделайте движение мышкой вниз через зеленый элемент. Если у вас получилось достаточно быстро, то на родительском элементе не было сгенерировано никаких событий.
6969
7070
[codetabs height=360 src="mouseoverout-fast"]
7171
```
7272

73-
## "Extra" mouseout when leaving for a child
73+
## "Лишний" mouseout при уходе на потомка
7474

75-
Imagine -- a mouse pointer entered an element. The `mouseover` triggered. Then the cursor goes into a child element. The interesting fact is that `mouseout` triggers in that case. The cursor is still in the element, but we have a `mouseout` from it!
75+
Представьте ситуацию -- курсор мыши зашёл на элемент. Сгенерировано событие `mouseover`. Затем курсор перешёл на дочерний элемент. Интересно, что в таком случае будет сгенерировано `mouseout`. То есть курсор всё ещё на элементе, но мы получили `mouseout`!
7676

7777
![](mouseover-to-child.png)
7878

79-
That seems strange, but can be easily explained.
79+
Это выглядит странно, но легко объясняется.
8080

81-
**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).**
81+
**По логике браузера, курсор мыши может быть только над одним элементом в любой момент времени - над самым глубоко вложенным (и имеющим самое большое значение z-index).**
8282

83-
So if it goes to another element (even a descendant), then it leaves the previous one. That simple.
83+
Таким образом, если курсор переходит на другой элемент (пусть даже дочерний), то он покидает предыдущий. Достаточно просто.
8484

85-
There's a funny consequence that we can see on the example below.
85+
На примере ниже можно увидеть забавную последовательность сгенерированных событий.
8686

87-
The red `<div>` is nested inside the blue one. The blue `<div>` has `mouseover/out` handlers that log all events in the textarea below.
87+
Красный `<div>` находится внутри синего. На синем `<div>`-е определены обработчики событий `mouseover/-out`, которые отображают всю информацию по ним в текстовое поле ниже.
8888

89-
Try entering the blue element and then moving the mouse on the red one -- and watch the events:
89+
Попробуйте зайти курсором на синий элемент, а затем перейдите на красный -- и смотрите, какие события сгенерировались:
9090

9191
[codetabs height=360 src="mouseoverout-child"]
9292

93-
1. On entering the blue one -- we get `mouseover [target: blue]`.
94-
2. Then after moving from the blue to the red one -- we get `mouseout [target: blue]` (left the parent).
95-
3. ...And immediately `mouseover [target: red]`.
93+
1. При входе на синий элемент -- мы получили `mouseover [target: blue]`.
94+
2. Затем при переходе на красный -- `mouseout [target: blue]` (уход с родителя).
95+
3. ...и сразу же `mouseover [target: red]`.
9696

97-
So, for a handler that does not take `target` into account, it looks like we left the parent in `mouseout` in `(2)` and returned back to it by `mouseover` in `(3)`.
97+
Таким образом, для обработчика, который не принимает во внимание свойство `target`, ситуация выглядит так, как будто курсор ушёл с родительского элемента, запустив `mouseout` на строке `(2)`, а затем вернулся обратно, запустив `mouseover` на строке `(3)`.
9898

99-
If we perform some actions on entering/leaving the element, then we'll get a lot of extra "false" runs. For simple stuff that may be unnoticeable. For complex things that may bring unwanted side-effects.
99+
Если постоянно передвигать курсор мышки между элементами, то мы получим много "ложных" срабатываний обработчиков. В простых случаях это может быть незаметно для пользователя, но в сложных случаях могут проявляться нежелательные побочные эффекты.
100100

101-
We can fix it by using `mouseenter/mouseleave` events instead.
101+
Чтобы их избежать, можно использовать события `mouseenter/mouseleave`.
102102

103103
## Events mouseenter and mouseleave
104104

0 commit comments

Comments
 (0)