简体   繁体   English

带有jQuery的子菜单无法正常工作

[英]Submenu with jquery is not working as it should

My process: 我的过程:

I´m using jQuery for the hover effect, when I hover the mouse in list item I want to find for a submenu and then I use the fadeToggle effect. 我将jQuery用于悬停效果,当我将鼠标悬停在要查找子菜单的列表项中时,我会使用fadeToggle效果。

Its like, When I hover my mouse it will look for children list items and if its displayed it will hide if its hidden it will display. 就像,当我将鼠标悬停在它上面时,它将寻找子项列表项;如果显示,则将其隐藏;如果隐藏,则将其显示。

And I use the stop method to avoid some strange effects if people pass very fast over the menu over and over again. 如果人们一遍又一遍地快速通过菜单,我将使用stop方法来避免一些奇怪的影响。

My issue: 我的问题:

I think I thought correctly but unfortunately I´m having a issue. 我想我想的没错,但是很不幸,我遇到了问题。

The submenu items are appear behind other elements, behind the #banner-container, so the submenu items are not visible. 子菜单项显示在其他元素的后面,在#banner-container的后面,因此子菜单项不可见。

I have tried to play with the positions, but so far without success, does anyone know how to solve this problem? 我曾尝试过担任这些职位,但是到目前为止,没有成功,有人知道如何解决这个问题吗?

My jsfiddle where you can see my problem: 我的jsfiddle,您可以在其中看到我的问题:

http://jsfiddle.net/jcak/9EcnL/6/ http://jsfiddle.net/jcak/9EcnL/6/

html: HTML:

<nav id="menu">
    <ul>    
        <li><a href="index.html">Home</a>
            <ul>
                <li><a href="#">Link 1</a></li>
                <li><a href="#">Link 2</a></li>
                <li><a href="#">Link 3</a></li>
                <li><a href="#">Link 4</a></li>
            </ul>
        </li>
        <li><a href="procuts.html">Procuts</a>
            <ul>
                <li><a href="#">Link 1</a></li>
                <li><a href="#">Link 2</a></li>
                <li><a href="#">Link 3</a></li>
                <li><a href="#">Link 4</a></li>
            </ul>
        </li>
        <li><a href="#">About</a></li>
        <li><a href="#">Contacts</a></li> 
   </ul>
</nav>   

jQuery: jQuery的:

$('li').hover(function(){
    $(this).find('ul>li').stop().fadeToggle(300);
});

CSS: CSS:

#menu ul li ul li {display:none;}

#menu-container
{

    background:green;
    width:100%; 
    height:46px; 
    float:left;  
    z-index:7;
}

#menu
{
    width:960px; 
    height:auto; 
    margin:0 auto 0 auto;
}

#menu ul
{
    list-style-type:none;
}

#menu ul li 
{
    float:left; 
    height:46px;
    line-height:46px; 
    font-weight:300;

}

#menu ul li a
{
    text-decoration:none;
    color:#ccc;  
    display:block; 
    margin-right:5px; 
    height:46px; 
    line-height:46px; 
    padding:0 5px 0 5px;
    font-size:20px; 

}

#menu ul li a:hover
{
    color:#fff;
}

You probably want something like this, right? 您可能想要这样的东西,对吧?

http://jsfiddle.net/9EcnL/3/ http://jsfiddle.net/9EcnL/3/

Your javascript was fine, you just forgot to add in the jQuery library in the jsfiddle options. 您的JavaScript很好,您只是忘了在jsfiddle选项的jQuery库中添加。 I put in some CSS to make it so that the submenus displayed a little better. 我添加了一些CSS来使子菜单显示得更好。

#menu ul li 
{
    display: inline-block;
    float:left; 
    height:46px;
    position: relative;
    line-height:46px; 
    font-weight:300;

}

#menu ul li a
{
    text-decoration:none;
    color:#ccc;  
    display:block; 
    margin-right:5px; 
    height:46px; 
    line-height:46px; 
    padding:0 5px 0 5px;
    font-size:20px; 

}

#menu ul li a:hover
{
    color:#fff;
}
#menu ul li ul {
    position: absolute;
    left: 0;
    -webkit-padding-start: 0;
    width: 300px;
}
#menu ul li ul li
{
    color:#fff;
}

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

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