Skip to content

Commit a52a643

Browse files
committed
minor
1 parent 01352c1 commit a52a643

File tree

3 files changed

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

3 files changed

+23
-11
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ Here's an example of code that accounts for all possible situations:
195195

196196
[js src="mouseenter-mouseleave-delegation-2/script.js"]
197197

198+
Once again, the important features are:
199+
1. It uses event delegation to handle entering/leaving of any `<td>` inside the table. So it relies on `mouseover/out` instead of `mouseenter/leave` that don't bubble and hence allow no delegation.
200+
2. Extra events, such as moving between descendants of `<td>` are filtered out, so that `onEnter/Leave` runs only if the pointer leaves or enters `<td>` as a whole.
201+
198202
```online
199203
Here's the full example with all details:
200204

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ table.onmouseover = function(event) {
1818

1919
// hooray! we entered a new <td>
2020
currentElem = target;
21-
target.style.background = 'pink';
22-
23-
// show that in textarea
24-
text.value += `OVER -> ${currentElem.tagName}.${currentElem.className}\n`;
25-
text.scrollTop = 1e6;
21+
onEnter(currentElem);
2622
};
2723

2824

@@ -44,11 +40,23 @@ table.onmouseout = function(event) {
4440
}
4541

4642
// we left the <td>. really.
47-
currentElem.style.background = '';
43+
onLeave(currentElem);
44+
currentElem = null;
45+
};
46+
47+
// any functions to handle entering/leaving an element
48+
function onEnter(elem) {
49+
elem.style.background = 'pink';
4850

4951
// show that in textarea
50-
text.value += `OUT <- ${currentElem.tagName}.${currentElem.className}\n`;
52+
text.value += `over -> ${currentElem.tagName}.${currentElem.className}\n`;
5153
text.scrollTop = 1e6;
54+
}
5255

53-
currentElem = null;
54-
};
56+
function onLeave(elem) {
57+
elem.style.background = '';
58+
59+
// show that in textarea
60+
text.value += `out <- ${elem.tagName}.${elem.className}\n`;
61+
text.scrollTop = 1e6;
62+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ table.onmouseover = function(event) {
22
let target = event.target;
33
target.style.background = 'pink';
44

5-
text.value += `OVER -> ${target.tagName}.${target.className}\n`;
5+
text.value += `over -> ${target.tagName}\n`;
66
text.scrollTop = text.scrollHeight;
77
};
88

99
table.onmouseout = function(event) {
1010
let target = event.target;
1111
target.style.background = '';
1212

13-
text.value += `OUT <- ${target.tagName}.${target.className}\n`;
13+
text.value += `out <- ${target.tagName}\n`;
1414
text.scrollTop = text.scrollHeight;
1515
};

0 commit comments

Comments
 (0)