Skip to content

Commit 92d6dc5

Browse files
committed
minor
1 parent 420ab39 commit 92d6dc5

File tree

2 files changed

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

2 files changed

+21
-21
lines changed

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,15 @@ The `mouseover` event on a descendant bubbles up. So, if the parent element has
9292
9393
![](mouseover-bubble-nested.svg)
9494
95-
So, when we move from a parent element to a child, then two handlers trigger on the parent element: `mouseout` and `mouseover`:
95+
```online
96+
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.
97+
98+
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).
99+
100+
[codetabs height=360 src="mouseoverout-child"]
101+
```
102+
103+
When we move from a parent element to a child, then two handlers trigger on the parent element: `mouseout` and `mouseover`:
96104

97105
```js
98106
parent.onmouseout = function(event) {
@@ -105,14 +113,6 @@ parent.onmouseover = function(event) {
105113

106114
If the code inside the handlers doesn't look at `target`, then it might think that the mouse left the `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.
107115

108-
```online
109-
In the example below the `<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.
110-
111-
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).
112-
113-
[codetabs height=360 src="mouseoverout-child"]
114-
```
115-
116116
If there's some action upon leaving the element, e.g. animation runs, then such interpretation may bring unwanted side effects.
117117

118118
To avoid it, we can check `relatedTarget` and, if the mouse is still inside the element, then ignore such event.
@@ -125,7 +125,7 @@ Events `mouseenter/mouseleave` are like `mouseover/mouseout`. They trigger when
125125

126126
But there are two important differences:
127127

128-
1. Transitions inside the element are not counted.
128+
1. Transitions inside the element, to/from descendants, are not counted.
129129
2. Events `mouseenter/mouseleave` do not bubble.
130130

131131
These events are extremely simple.
@@ -154,7 +154,7 @@ Handlers for `mouseenter/leave` on `<table>` only trigger when the pointer enter
154154

155155
So, let's use `mouseover/mouseout`.
156156

157-
Let's start with handlers that highlight the element under mouse:
157+
Let's start with simple handlers that highlight the element under mouse:
158158

159159
```js
160160
// let's highlight an element under the pointer
@@ -203,5 +203,7 @@ These things are good to note:
203203

204204
- A fast mouse move may skip intermediate elements.
205205
- Events `mouseover/out` and `mouseenter/leave` have an additional property: `relatedTarget`. That's the element that we are coming from/to, complementary to `target`.
206-
- Events `mouseover/out` trigger even when we go from the parent element to a child element. The browser assumes that the mouse can be only over one element at one time -- the deepest one.
207-
- Events `mouseenter/leave` do not bubble and do not trigger when the mouse goes to a child element. They only track whether the mouse comes inside and outside the element as a whole. So, they are simpler than `mouseover/out`, but we can't implement delegation using them.
206+
207+
Events `mouseover/out` trigger even when we go from the parent element to a child element. The browser assumes that the mouse can be only over one element at one time -- the deepest one.
208+
209+
Events `mouseenter/leave` are different in that aspect: they only trigger when the mouse comes in and out the element as a whole. Also they do not bubble.

2-ui/3-event-details/3-mousemove-mouseover-mouseout-mouseenter-mouseleave/mouseenter-mouseleave-delegation-2.view/script.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,15 @@ table.onmouseout = function(event) {
2828
// e.g. from <tr> to another <tr>
2929
if (!currentElem) return;
3030

31-
// we're leaving the element -- where to? Maybe to a descendant?
31+
// we're leaving the element where to? Maybe to a descendant?
3232
let relatedTarget = event.relatedTarget;
3333

34-
if (relatedTarget) { // possible: relatedTarget = null
35-
while (relatedTarget) {
36-
// go up the parent chain and check -- if we're still inside currentElem
37-
// then that's an internal transition -- ignore it
38-
if (relatedTarget == currentElem) return;
34+
while (relatedTarget) {
35+
// go up the parent chain and check – if we're still inside currentElem
36+
// then that's an internal transition – ignore it
37+
if (relatedTarget == currentElem) return;
3938

40-
relatedTarget = relatedTarget.parentNode;
41-
}
39+
relatedTarget = relatedTarget.parentNode;
4240
}
4341

4442
// we left the <td>. really.

0 commit comments

Comments
 (0)