简体   繁体   English

jQuery下拉导航

[英]Jquery Drop Down Navigation

I got this kind of Navigation: 我得到了这种导航:

<ul>
<li><a href="#">Navi 1</a>
<ul class="navisub" style="display:none">
 <li>Subnavi 1</li>
 <li>Subnavi 2</li>
</ul>
</li>
<li><a href="#">Navi 2</a>
<ul class="navisub" style="display:none">
 <li>Subnavi 3</li>
 <li>Subnavi 2</li>
</ul>
</li>
</ul>

Mainproblem is, when I hover the "Navi 1" li, the subnavi should show. 主要问题是,当我将“ Navi 1” li悬停时,subnavi应该显示。 But when i enter the subnavi with my cursor everything is hiding. 但是当我用光标进入子导航时,一切都隐藏了。

thats my code: 那就是我的代码:

$('#navi li').mouseenter(
    function () {
        //show its submenu
        $('ul.navisub', this).css('display','block');

    }).mouseleave( 
    function () {
        //hide its submenu
        $('ul.navisub', this).css('display','none');         
    }
);

Any suggestions? 有什么建议么? Thank you all very much! 非常感谢大家!

I had the same problem some time ago and I solved it using hover (also use show/hide): 不久前我遇到了同样的问题,我使用hover (也使用show / hide)解决了这个问题:

$('#navi li').hover(
    function () {
        //show its submenu
        $('ul.navisub', this).show();
    },
    function () {
        //hide its submenu
        $('ul.navisub', this).hide();         
    }
);

You can add a bit of animation too. 您也可以添加一些动画。 Using the toggleFade 使用toggleFade

$('#navi li').hover(function() {
$(this).find('ul.navisub').stop(true, true).fadeToggle(600); 
});

Notice the .stop method, this will stop the animation from queuing, from hovering on/off faster than the animation time. 请注意.stop方法,这将使动画停止排队,其悬停开/关的速度比动画时间快。 You can change the (600) to whatever you'd like at the moment it's set to 0.6 seconds as its in milliseconds. 您可以将(600)更改为您想要的任意值,将其设置为0.6秒(以毫秒为单位)。

Checkout the jsFiddle 检出jsFiddle

Depending on your target browser requirements it is possible to do this without JavaScript. 根据您的目标浏览器要求,可以在不使用JavaScript的情况下执行此操作。 See a pure CSS demo here . 在这里查看纯CSS 演示

If you need something the works in IE6 then jQuery is a good solution. 如果您需要IE6中的某些功能,那么jQuery是一个很好的解决方案。 I can expand on this with a jQuery version if needed :) 如果需要,我可以使用jQuery版本对此进行扩展:)

Edit: jQuery version here . 编辑: 这里的jQuery版本

Second Edit: New version which might do what you want. 第二编辑:可能会做您想要的新版本

Given this, I presume you meant for your <ul> to have an id of "navi"? 鉴于此,我想您是要让您的<ul>的ID为“ navi”吗?

<ul>
    <li><a href="#">Navi 1</a>
    <ul class="navisub" style="display:none">...



$('#navi li').mouseenter(
    function () {...

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

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