Introduction
When a user interacts with a pointing device (such as a mouse), the events are known as MouseEvents. Click, dblclick, mouseup, and mousedown are all common events in this interface. MouseEvent is derived from UIEvent, which in turn is derived from Event.
Such events can occur on devices other than the mouse, such as phones and tablets, where they are emulated for compatibility. This blog will go over mouse events such as mouseover/out/enter and their properties in greater depth. So without any further delay, let’s dive deeper into the topic!
Also Read, Javascript hasOwnProperty
Mouseover and Mouseout
Source: Kirupa
When a mouse cursor passes over an element, the mouseover event occurs, and when it passes over an element, the mouseout event occurs.
These events are unique in that they have the relatedTarget property. This property is useful in conjunction with target. When a mouse moves from one element to another, one becomes the target, while the other becomes relatedTarget.
For the mouseover event:
- The element where the mouse landed is called Event.target.
- The element from which the mouse originated (relatedTarget target) is represented by Event.relatedTarget.
The reverse is true for mouseout:
- The element that the mouse left is represented by event.target.
- The new under-the-pointer element that the mouse left for (target relatedTarget) is event.relatedTarget.
Note: relatedTarget may or may not be null.
It is possible for the relatedTarget property to be null.
That's normal, because it only signifies the mouse came from outside the window, not from another element. Or that it had walked away from the window.
When utilising Event.relatedTarget in our code, we should keep that option in mind. There will be an error if we try to access Event.relatedTarget.tagName.