"Smart" tooltip
Write a function that shows a tooltip over an element only if the visitor moves the mouse to it, but not through it.
In other words, if the visitor moves the mouse to the element and stops there â show the tooltip. And if they just moved the mouse through, then no need, who wants extra blinking?
Technically, we can measure the mouse speed over the element, and if itâs slow then we assume that it comes âover the elementâ and show the tooltip, if itâs fast â then we ignore it.
Make a universal object new HoverIntent(options) for it.
Its options:
elemâ element to track.overâ a function to call if the mouse came to the element: that is, it moves slowly or stopped over it.outâ a function to call when the mouse leaves the element (ifoverwas called).
An example of using such object for the tooltip:
// a sample tooltip
let tooltip = document.createElement('div');
tooltip.className = "tooltip";
tooltip.innerHTML = "Tooltip";
// the object will track mouse and call over/out
new HoverIntent({
elem,
over() {
tooltip.style.left = elem.getBoundingClientRect().left + 'px';
tooltip.style.top = elem.getBoundingClientRect().bottom + 5 + 'px';
document.body.append(tooltip);
},
out() {
tooltip.remove();
}
});
The demo:
If you move the mouse over the âclockâ fast then nothing happens, and if you do it slow or stop on them, then there will be a tooltip.
Please note: the tooltip doesnât âblinkâ when the cursor moves between the clock subelements.
The algorithm looks simple:
- Put
onmouseover/outhandlers on the element. Also can useonmouseenter/leavehere, but they are less universal, wonât work if we introduce delegation. - When a mouse cursor entered the element, start measuring the speed on
mousemove. - If the speed is slow, then run
over. - When weâre going out of the element, and
overwas executed, runout.
But how to measure the speed?
The first idea can be: run a function every 100ms and measure the distance between previous and new coordinates. If itâs small, then the speed is small.
Unfortunately, thereâs no way to get âcurrent mouse coordinatesâ in JavaScript. Thereâs no function like getCurrentMouseCoordinates().
The only way to get coordinates is to listen for mouse events, like mousemove, and take coordinates from the event object.
So letâs set a handler on mousemove to track coordinates and remember them. And then compare them, once per 100ms.
P.S. Please note: the solution tests use dispatchEvent to see if the tooltip works right.