` only trigger on entering/leaving the whole table. It's impossible to get any information about transitions inside it.
Not a problem -- let's use `mouseover/mouseout`.
A simple handler may look like this:
```js
// let's highlight cells under mouse
table.onmouseover = function(event) {
let target = event.target;
target.style.background = 'pink';
};
table.onmouseout = function(event) {
let target = event.target;
target.style.background = '';
};
```
```online
[codetabs height=480 src="mouseenter-mouseleave-delegation"]
```
These handlers work when going from any element to any inside the table.
But we'd like to handle only transitions in and out of `` as a whole. And highlight the cells as a whole. We don't want to handle transitions that happen between the children of ` | `.
One of solutions:
- Remember the currently highlighted ` | ` in a variable.
- On `mouseover` -- ignore the event if we're still inside the current ` | `.
- On `mouseout` -- ignore if we didn't leave the current ` | `.
That filters out "extra" events when we are moving between the children of ` | `.
```offline
The details are in the [full example](sandbox:mouseenter-mouseleave-delegation-2).
```
```online
Here's the full example with all details:
[codetabs height=380 src="mouseenter-mouseleave-delegation-2"]
Try to move the cursor in and out of table cells and inside them. Fast or slow -- doesn't better. Only ` | ` as a whole is highlighted unlike the example before.
```
## Summary
We covered events `mouseover`, `mouseout`, `mousemove`, `mouseenter` and `mouseleave`.
Things that are good to note:
- A fast mouse move can make `mouseover, mousemove, mouseout` to skip intermediate elements.
- Events `mouseover/out` and `mouseenter/leave` have an additional target: `relatedTarget`. That's the element that we are coming from/to, complementary to `target`.
- Events `mouseover/out` trigger even when we go from the parent element to a child element. They assume that the mouse can be only over one element at one time -- the deepest one.
- 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.
|