How to use hoverIntent plugin?
Asked Answered
R

2

13

I'm new to jQuery and want to add the hoverIntent plugin to my site as my navigation menu. I have been referred to Brian Cherne's site and see the code to download, but I'm not quite sure how to put it all together to make it work.

Can someone post a sample of what the HTML code should look like with the appropriate hoverIntent plugin code added in? Or refer me to a reference?

I'd be most appreciative! Thanks!

Rafaellle answered 6/3, 2013 at 2:0 Comment(0)
A
14

hoverIntent plugin follows the same exact api signature of the jQuery hover method. You can get usage examples at http://cherne.net/brian/resources/jquery.hoverIntent.html, just look at the source code.

First include jQuery into the head:

<script type="text/javascript" src="http://code.jquery.com/jquery.js"></script>

after that download and include hoverIntent plugin:

<script type="text/javascript" src="path/to/jquery.hoverIntent.js"></script>

then you can use hoverIntent() method on any jQuery element like this

$(element).hoverIntent(whatToDoWhenHover, whatToDoWhenOut);

element is a jQuery selector like '#id' or '.class' or 'div > .something', whatToDoWhenHover and whatToDoWhenOut are functions that will be executed when the user starts hovering the element and when he stops. Just like old good jQuery hover.

Example

Anuria answered 6/3, 2013 at 2:2 Comment(7)
I'm sorry but I don't have a basic understanding of jQuery yet so I'm lost when it comes to figuring out the provided code. I get that I'm supposed to use the code I'm just not sure where to put it.Rafaellle
Thanks the example. I guess I'm still just not sure how to do this. :-/Rafaellle
Thanks for spelling it out for me. I appreciate it. Where do I put this piece of code? $(element).hoverIntent(whatToDoWhenHover, whatToDoWhenOut);Rafaellle
I still don't have it yet but I'm farther along than I was at the start so thanks for that. If I want to create a menu via creating a list do I insert <ul li a> as the element? I assume the whatToDoWhenHover and whatToDoWhenOut are explained in more detail in Cherne's code?Rafaellle
So within the <head> tag??Rafaellle
Yes, even if it's not considered best practice, it will work. If you are in doubt, look at the source of hoverIntent page or at my example on jsFiddle, is as easy as pressing CTRL+U in Firefox. Reading webpages source written by others is imho the best way to learn how to do thing in html+jsAnuria
Thanks for the help. It's a start for sure. I'm still not quite getting it but I think it's going to just take some more trial and error on my part. Thanks!Rafaellle
B
2

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);
Borax answered 6/2, 2020 at 14:30 Comment(1)
Great. Is it a perfect port of the original jquery-hoverintent? I mean, in term of functionality. I think maybe it lacks the ability to pass a selector for event delegation? P.s. you schout put it in github! :)Schiff

© 2022 - 2024 — McMap. All rights reserved.