简体   繁体   English

Hoverintent / Hover Delay jQuery

[英]Hoverintent/Hover Delay jQuery

Hi all I'm trying to accomplish a few things. 大家好,我正在尝试完成一些事情。

I have an element that is displayed one mouse over, it's essentially a submenu but it is not structured like your traditional submenu would be in that it is no within an 'li' element. 我有一个显示在鼠标上的元素,它本质上是一个子菜单,但是它的结构不像您的传统子菜单那样,因为它不在'li'元素内。 What I'm attempting to do is when a user hovers over 'products' the subnav is displayed - this works without issue. 我想做的是,当用户将鼠标悬停在“产品”上时,将显示子导航-可以正常工作。 However when the user moves their mouse from 'products' to the subnav menu itself I want the submenu to remain and not disappear until both elements (a#products and #banner-top) no longer have a mouseover. 但是,当用户将鼠标从“产品”移动到子导航菜单本身时,我希望子菜单保留并且不消失,直到两个元素(a#products和#banner-top)都不再具有鼠标悬停。

I'm currently using hoverintent to accomplish this because it sounded like it would suit my purposes. 我目前正在使用hoverintent来完成此操作,因为听起来这很适合我的目的。 I was under the impression the 'out' would not be called just so long as the user remained hovering over one of the elements that the .hoverintent is attached to. 我的印象是,只要用户仍将鼠标悬停在.hoverintent所附加的元素之一上,就不会调用“ out”。 I also assumed that the 'out' would not trigger even if the user hovers off the initial element that triggered the '#product-sub-nav' to display just so long as they did it in a short period of time. 我还假设,即使用户将触发“#product-sub-nav”的初始元素悬停在短时间内显示的初始元素上,也不会触发“ out”。 In other words, the user hovers over 'products' the submenu displays then the user hovers over the submenu in a short period of time thus not triggering the function that attaches a 'hidden' class to the subnav to hide it again. 换句话说,用户将鼠标悬停在子菜单显示的“产品”上,然后用户在短时间内将鼠标悬停在子菜单上,因此不会触发将“隐藏”类附加到子导航的功能以再次将其隐藏。 I hope I've done a decent job of explaining what I'm trying to do. 我希望我在解释自己想做的事情上做得不错。

Here is my code 这是我的代码

    var settings = {
                sensitivity: 4,
                interval: 75,
                timeout: 500,
                over: mousein_trigger,
                out: mouseout_trigger
            };

            jQuery('.item-134 a, #product-sub-nav').hoverIntent(settings);

            function mousein_trigger() {
                jQuery('#banner-top').removeClass('hidden')
            }
            function mouseout_trigger() {
                jQuery('#banner-top').addClass('hidden')
            }

UPDATE W/ JS FIDDLE 带有JS格式的更新

http://jsfiddle.net/M5BN2/ http://jsfiddle.net/M5BN2/

I just wanted to update this in case someone else had a similar issue. 我只是想更新它,以防其他人遇到类似问题。 This solution works perfectly: https://stackoverflow.com/a/1670561/1108360 该解决方案非常有效: https : //stackoverflow.com/a/1670561/1108360

jQuery(".item-134 a, #banner-top").mouseenter(function() { //if mouse is over 'products' link or submenu
                //clear timeout
                clearTimeout(jQuery(this).data('timeoutId'));
                //display sub menu
                jQuery('#banner-top').removeClass('hidden');
            }).mouseleave(function() { //when mouse leaves element
                timeoutId = setTimeout(function() {
                    //delay hiding sub menu
                    jQuery('#banner-top').addClass('hidden');
                }, 650);
                //set the timeoutId, allowing us to clear this trigger if the mouse comes back over
                jQuery(".item-134 a, #banner-top").data('timeoutId', timeoutId);
            });

Didn't properly update JSfiddle: http://jsfiddle.net/M5BN2/5/ 没有正确更新JSfiddle: http : //jsfiddle.net/M5BN2/5/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM