Skip to content

Commit 0e4f5e4

Browse files
committed
minor
1 parent 2f725bc commit 0e4f5e4

File tree

1 file changed

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

1 file changed

+24
-14
lines changed

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

+24-14
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ That means that if the visitor is moving the mouse very fast then some DOM-eleme
4646
4747
![](mouseover-mouseout-over-elems.svg)
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+
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`.
5050
51-
That's good for performance, because if there may be many intermediate elements. We don't really want to process in and out of each one.
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.
5252
5353
On the other hand, we should keep in mind that the mouse pointer doesn't "visit" all elements along the way. It can "jump".
5454
@@ -67,38 +67,46 @@ Also move the pointer into the child `div`, and then move it out quickly down th
6767
```
6868

6969
```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 with `mouseover`, then upon leaving it we always get `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`.
7171
```
7272
7373
## Mouseout when leaving for a child
7474
75-
An important feature of `mouseout` -- it triggers, when the pointer moves from an element to its descendant.
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:
7676
77-
Visually, the pointer is still on the element, but we get `mouseout`!
77+
```html
78+
<div id="parent">
79+
<div id="child">...</div>
80+
</div>
81+
```
82+
83+
If we're on `#parent` and then move the pointer deeper into `#child`, but we get `mouseout` on `#parent`!
7884

7985
![](mouseover-to-child.svg)
8086

81-
That looks strange, but can be easily explained.
87+
That may seem strange, but can be easily explained.
8288

8389
**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.**
8490

8591
So if it goes to another element (even a descendant), then it leaves the previous one.
8692

87-
Please note an important detail.
93+
Please note another important detail of event processing.
8894

89-
The `mouseover` event on a descendant bubbles up. So, if the parent element has such handler, it triggers.
95+
The `mouseover` event on a descendant bubbles up. So, if `#parent` has `mouseover` handler, it triggers:
9096

9197
![](mouseover-bubble-nested.svg)
9298

9399
```online
94-
You can see that very well in the example below: `<div id="child">` is inside the `<div id="parent">`. There are handlers on the parent that listen for `mouseover/out` events and output their details.
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.
95101
96-
If you move the mouse from the parent to the child, you see two events: `mouseout [target: parent]` (left the parent) and `mouseover [target: child]` (came to the child, bubbled).
102+
If you move the mouse from `#parent` to `#child`, you see two events on `#parent`:
103+
1. `mouseout [target: parent]` (left the parent), then
104+
2. `mouseover [target: child]` (came to the child, bubbled).
97105
98106
[codetabs height=360 src="mouseoverout-child"]
99107
```
100108

101-
When we move from a parent element to a child, then two handlers trigger on the parent element: `mouseout` and `mouseover`:
109+
As shown, when the pointer moves from `#parent` element to `#child`, two handlers trigger on the parent element: `mouseout` and `mouseover`:
102110

103111
```js
104112
parent.onmouseout = function(event) {
@@ -109,11 +117,13 @@ parent.onmouseover = function(event) {
109117
};
110118
```
111119

112-
If we don't examine `event.target` inside the handlers, then it may seem that the mouse pointer left `parent` element, and then came back over it. But it's not the case! The mouse never left, it just moved to the child element.
120+
**If we don't examine `event.target` inside the handlers, then it may seem that the mouse pointer left `#parent` element, and then immediately came back over it.**
121+
122+
But that's not the case! The pointer is still over the parent, it just moved deeper into the child element.
113123

114-
If there's some action upon leaving the element, e.g. animation runs, then such interpretation may bring unwanted side effects.
124+
If there are some actions upon leaving the parent element, e.g. an animation runs in `parent.onmouseout`, we usually don't want it when the pointer just goes deeper into `#parent`.
115125

116-
To avoid it, we can check `relatedTarget` and, if the mouse is still inside the element, then ignore such event.
126+
To avoid it, we can check `relatedTarget` in the handler and, if the mouse is still inside the element, then ignore such event.
117127

118128
Alternatively we can use other events: `mouseenter` и `mouseleave`, that we'll be covering now, as they don't have such problems.
119129

0 commit comments

Comments
 (0)