简体   繁体   English

Jquery / HTML下拉菜单悬停状态

[英]Jquery/HTML dropdown menu hover-state

I am trying to create a drop-down menu that will stay open on hover even when it is hovering over the drop-down div ('#units-menu-links'). 我正在尝试创建一个下拉菜单,即使它悬停在下拉div('#units-menu-links')上也会在悬停时保持打开状态。 I can't get it to stay open while the user is still hovering over the 'units-menu-links' div. 当用户仍然悬停在“units-menu-links”div上时,我无法让它保持打开状态。 Currently it does nothing, but before I had the .toggle inside the second function which would just make it disappear whenever the user wasn't hovering over the "#units-nav" div. 目前它没有做任何事情,但在第二个函数内部使用.toggle之前,只要用户没有悬停在“#units-nav”div上,它就会消失。

Here is what my HTML looks like: 这是我的HTML的样子:

<div id="units-menu-links">
    <center>
            <p class="units-menu-links-items" href="dynamically generated URL">Menu Item</p>
    </center>
</div>

<div id="menu">
        <div id="menu-inside">
                <div class="bar">
                        <div class="nav-block" id="units-nav">
                                <a href="/link/">Menu Button</a>
                        </div>
                </div>
        </div>
</div>

Here is my Jquery: 这是我的Jquery:

$('#units-menu-links').hide();
    $("#units-nav").hover(
            function () {
                // Over the hover.
                $('#units-menu-links').toggle();
            },
            function () {
                // PLEASE READ THE FOLLOWING COMMENTS TO UNDERSTAND THE FUNCTIONALITY I AM TRYING TO GET.
                // When the users leaves #units-nav and #units-menu-links
                // then do the "$('#units-menu-links').toggle();" again to hide this.
                // However if the user is hovering over "$('#units-menu-links').toggle();" then don't do anything.
            }
    );

(I've looked at quite a few questions and answers on SO as well as looking on Google, and can't find anything to help me with this problem. It seems like it is be a very common problem, but people have created their HTML structure differently to mine which is making it difficult to find resources.) (我已经看了很多关于SO的问题和答案以及在谷歌上看,并且找不到任何可以帮助我解决这个问题。看起来这是一个非常普遍的问题,但人们创造了他们的HTML结构与我的不同,这使得很难找到资源。)

Try hiding the menu when the mouse goes outside the submenu: 当鼠标移出子菜单时尝试隐藏菜单:

$('#units-menu-links').hide();
$("#units-nav").hover(
    function () {
        $('#units-menu-links').show();
    },
    function () {
    }
);
$("#units-menu-links").hover(
    function () {
    },
    function () {
        $('#units-menu-links').hide();
    }
);

​

Ideally, I would suggest not using JavaScript. 理想情况下,我建议不要使用JavaScript。

HTML: HTML:

<div id="menu" style="background-color: red">
    <div id="menu-inside">
        <div class="bar">
            <div class="nav-block" id="units-nav">
                <a href="/link/">Menu Button</a>
            </div>
        </div>
    </div>
    <div id="units-menu-links" style="background-color: green">
        <center>
            <p class="units-menu-links-items" href="dynamically generated URL">Menu Item</p>
        </center>
    </div>
</div>

​CSS: CSS:

#menu #units-menu-links {
    display: none;
}

#menu:hover #units-menu-links {
    display: block;
}
​

Here's the Fiddle . 这是小提琴

$('#units-menu-links').hide();
var timeOut;
$("#units-nav").hover(
    function () {
        // Over the hover.
        $('#units-menu-links').show();
        clearTimeout(timeOut);
    },
    function () {
        timeOut = setTimeout(function(){
            $('#units-menu-links').hide();
        }, 100);
    }
);

$('#units-menu-links').hover(
    function () {
        clearTimeout(timeOut);
    },
    function () {
        timeOut = setTimeout(function(){
            $('#units-menu-links').hide();
        }, 100);
    }
)

When using hover you should have your submenu nested under #units-nav element. 使用悬停时,您应该将子菜单嵌套在#units-nav元素下。 Then it will work. 然后它会工作。 If it is not possible to do so, you can implement some timeout to the second callback of your hover function and you can stop the timeout if the user will hover the #units-menu-links-element ..But this is kinda awful, users can get confused with that behavior. 如果无法这样做,你可以对你的悬停功能的第二个回调实现一些超时,如果用户将鼠标悬停在#units-menu-links-element ,你可以停止超时。但这有点糟糕,用户可能会对此行为感到困惑。

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

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