The 2 event occurs when a mouse pointer comes over an element, and 3 – when it leaves. Show These events are special, because they have property 4. This property complements 5. When a mouse leaves one element for another, one of them becomes 5, and the other one – 4.For 2:
For 3 the reverse:
In the example below each face and its features are separate elements. When you move the mouse, you can see mouse events in the text area. Each event has the information about both 5 and 4:Result script.js style.css index.html
The 4 property can be 1.That’s normal and just means that the mouse came not from another element, but from out of the window. Or that it left the window. We should keep that possibility in mind when using 0 in our code. If we access 5, then there will be an error.The 6 event triggers when the mouse moves. But that doesn’t mean that every pixel leads to an event.The browser checks the mouse position from time to time. And if it notices changes then triggers the events. That means that if the visitor is moving the mouse very fast then some DOM-elements may be skipped: If the mouse moves very fast from 7 to 8 elements as painted above, then intermediate 9 elements (or some of them) may be skipped. The 3 event may trigger on 7 and then immediately 2 on 8.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. On the other hand, we should keep in mind that the mouse pointer doesn’t “visit” all elements along the way. It can “jump”. In particular, it’s possible that the pointer jumps right inside the middle of the page from out of the window. In that case 4 is 1, because it came from “nowhere”:You can check it out “live” on a teststand below. Its HTML has two nested elements: the 6 is inside the 7. If you move the mouse fast over them, then maybe only the child div triggers events, or maybe the parent one, or maybe there will be no events at all.Also move the pointer into the child 8, and then move it out quickly down through the parent one. If the movement is fast enough, then the parent element is ignored. The mouse will cross the parent element without noticing it.Result script.js style.css index.html
If 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 ( 2 event generated), then upon leaving it we always get 3.An important feature of 3 – it triggers, when the pointer moves from an element to its descendant, e.g. from 4 to 5 in this HTML:
If we’re on 4 and then move the pointer deeper into 5, we get 3 on 4!That may seem strange, but can be easily explained. 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. So if it goes to another element (even a descendant), then it leaves the previous one. Please note another important detail of event processing. The 2 event on a descendant bubbles up. So, if 4 has 2 handler, it triggers:You can see that very well in the example below: 6 is inside the 7. There are 5 handlers on 4 element that output event details.If you move the mouse from 4 to 5, you see two events on 4:
Result script.js style.css index.html
As shown, when the pointer moves from 4 element to 5, two handlers trigger on the parent element: 3 and 2: 0If we don’t examine 9 inside the handlers, then it may seem that the mouse pointer left 4 element, and then immediately came back over it.But that’s not the case! The pointer is still over the parent, it just moved deeper into the child element. If there are some actions upon leaving the parent element, e.g. an animation runs in 8, we usually don’t want it when the pointer just goes deeper into 4.To avoid it, we can check 4 in the handler and, if the mouse is still inside the element, then ignore such event.Alternatively we can use other events: 1 and 2, that we’ll be covering now, as they don’t have such problems.Events 3 are like 4. They trigger when the mouse pointer enters/leaves the element.But there are two important differences:
These events are extremely simple. When the pointer enters an element – 1 triggers. The exact location of the pointer inside the element or its descendants doesn’t matter.When the pointer leaves an element – 2 triggers.This example is similar to the one above, but now the top element has 3 instead of 4.As you can see, the only generated events are the ones related to moving the pointer in and out of the top element. Nothing happens when the pointer goes to the child and back. Transitions between descendants are ignored Result script.js style.css index.html
3Events 00 are very simple and easy to use. But they do not bubble. So we can’t use event delegation with them.Imagine we want to handle mouse enter/leave for table cells. And there are hundreds of cells. The natural solution would be – to set the handler on 01 and process events there. But 00 don’t bubble. So if such event happens on 03, then only a handler on that 03 is able to catch it.Handlers for 00 on 01 only trigger when the pointer enters/leaves the table as a whole. It’s impossible to get any information about transitions inside it.So, let’s use 4.Let’s start with simple handlers that highlight the element under mouse: 4Here they are in action. As the mouse travels across the elements of this table, the current one is highlighted: Result script.js style.css index.html 5 6 7In our case we’d like to handle transitions between table cells 03: entering a cell and leaving it. Other transitions, such as inside the cell or outside of any cells, don’t interest us. Let’s filter them out.Here’s what we can do:
Here’s an example of code that accounts for all possible situations: 8Once again, the important features are:
Here’s the full example with all details: Result script.js style.css index.html 8 6 7Try to move the cursor in and out of table cells and inside them. Fast or slow – doesn’t matter. Only 03 as a whole is highlighted, unlike the example before.We covered events 2, 3, 6, 1 and 2.These things are good to note:
Events 5 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.Events 00 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. |