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
Copy file name to clipboardExpand all lines: 2-ui/3-event-details/3-mousemove-mouseover-mouseout-mouseenter-mouseleave/article.md
+15-13Lines changed: 15 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -92,7 +92,15 @@ The `mouseover` event on a descendant bubbles up. So, if the parent element has
92
92
93
93

94
94
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`:
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.
107
115
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
-
116
116
If there's some action upon leaving the element, e.g. animation runs, then such interpretation may bring unwanted side effects.
117
117
118
118
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
125
125
126
126
But there are two important differences:
127
127
128
-
1. Transitions inside the element are not counted.
128
+
1. Transitions inside the element, to/from descendants, are not counted.
129
129
2. Events `mouseenter/mouseleave` do not bubble.
130
130
131
131
These events are extremely simple.
@@ -154,7 +154,7 @@ Handlers for `mouseenter/leave` on `<table>` only trigger when the pointer enter
154
154
155
155
So, let's use `mouseover/mouseout`.
156
156
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:
158
158
159
159
```js
160
160
// let's highlight an element under the pointer
@@ -203,5 +203,7 @@ These things are good to note:
203
203
204
204
- A fast mouse move may skip intermediate elements.
205
205
- 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.
Copy file name to clipboardExpand all lines: 2-ui/3-event-details/3-mousemove-mouseover-mouseout-mouseenter-mouseleave/mouseenter-mouseleave-delegation-2.view/script.js
0 commit comments