if you wish to use hoverIntent functionality without being dependent to jQuery, you can use the Pure JavaScript ES6 version of it (or convert it to es5 with ease).
const hoverIntent = (el, onOver, onOut) => {
let x;
let y;
let pX;
let pY;
const h = {};
let state = 0;
let timer = 0;
let options = {
sensitivity: 7,
interval: 100,
timeout: 0,
handleFocus: false,
overClass: 'hovered'
};
const delay = e => {
if (timer) timer = clearTimeout(timer);
state = 0;
if (onOut) {
return onOut.call(el, e);
}
el.classList.remove(options.overClass);
return false;
};
const tracker = e => {
x = e.clientX;
y = e.clientY;
};
const compare = e => {
if (timer) timer = clearTimeout(timer);
if (Math.abs(pX - x) + Math.abs(pY - y) < options.sensitivity) {
state = 1;
if (onOver) {
return onOver.call(el, e);
}
el.classList.add(options.overClass);
return false;
}
pX = x;
pY = y;
timer = setTimeout(() => {
compare(e);
}, options.interval);
return false;
};
// Public methods
const dispatchOver = e => {
if (timer) timer = clearTimeout(timer);
el.removeEventListener('mousemove', tracker, false);
if (state !== 1) {
pX = e.clientX;
pY = e.clientY;
el.addEventListener('mousemove', tracker, false);
timer = setTimeout(() => {
compare(e);
}, options.interval);
}
return this;
};
const dispatchOut = e => {
if (timer) timer = clearTimeout(timer);
el.removeEventListener('mousemove', tracker, false);
if (state === 1) {
timer = setTimeout(() => {
delay(e);
}, options.timeout);
}
return this;
};
if (el) {
el.addEventListener('mouseover', dispatchOver, false);
el.addEventListener('mouseout', dispatchOut, false);
}
h.options = opt => {
options = { ...options, ...opt };
return h;
};
h.remove = () => {
if (!el) return;
el.removeEventListener('mouseover', dispatchOver, false);
el.removeEventListener('mouseout', dispatchOut, false);
};
return h;
};
usage:
const menuEl = document.querySelector('.menu');
hoverIntent(menuEl);
this will add "hovered" class to menu element, then you can use plain CSS to enable/disable child-dropdown-boxes when parent-menu-item is hovered
- TIP: instead of running hoverIntent for each menu-items; running it only for 1 element (i.e: menu-wrapper element) and adding simple CSS hover rule for parent-menu-items elements to display dropdown boxes; based on menu-wrapper has already been hovered or not, will be much more performance efficient ;) *
The css will be something similar to;
.menu.hovered .parent-li:hover {
background-color: #f4f4f4;
}
.menu.hovered .parent-li:hover .child {
display: block;
}
I created a playground, see the live demo:
https://codepen.io/mcakir/full/OJVJmdV
Advanced usage: hoverIntent
method accepts onOver
and onOut
as well as to be extended options
.
example:
const onOverHandler = () => {
console.log('mouse in!');
// do something
}
const onOutHandler = () => {
console.log('mouse out!');
// do something
}
const customOptions = () => {
sensitivity: 7,
interval: 300,
timeout: 200,
}
const menuEl = document.querySelector('.menu');
hoverIntent(menuEl, onOverHandler, onOutHandler).options(customOptions);