带有悬停父项的下拉菜单

时间:2012-07-15 13:50:57

标签: css drop-down-menu menu

我想要简单的下拉菜单。但我有一个问题。我希望当我将鼠标悬停在丢弃的项目(drop-menu-items)上时,我的父元素(drop-menu-parent)将用黑色着色,但它是白色的。我无法解释得很清楚。你可以在这里看到它 - > http://jsfiddle.net/YEyuP/

3 个答案:

答案 0 :(得分:3)

我认为这就是您所需要的:http://jsfiddle.net/YEyuP/6/

HTML:

<nav>
        <ul id="cat-nav">
            <li><a href="#">About</a></li>
            <li><a href="#">Contact</a></li>
            <li class="drop-menu-parent">
                <a href="#">Portfolio</a>

                <ul class="drop-menu-items">
                    <li><a href="#" title="Some other category">Some other category</a></li>
                    <li><a href="#" title="Some other category">Some other category</a></li>
                    <li><a href="#" title="Some other category">Some other category</a></li>
                    <li><a href="#" title="Some other category">Some other category</a></li>
                </ul>
            </li>
            <li><a href="#">Testimonials</a></li>
            <li><a href="#">Browse Products</a></li>
            <li><a href="#">Support Forum</a></li>            

        </ul>
    </nav>
​

CSS:

nav {
    background: #000;
}

#cat-nav {
    text-align: center;
    background-color: #0f0609, #0f0609;
    background-image: url(cat-nav-bg.jpg), url(cat-nav-bg-bottom.jpg);
    background-repeat: no-repeat, repeat-y;
    margin-top: -8px;
    display: block;
}

#cat-nav:after {
    content: " ";
    display: block;
    clear: both;

    width: 81%;
    padding-top: 8px;
    margin: 0 auto;
    background: url(teeth.png) repeat-x;
}

ul#cat-nav > li {
    text-align: center;
    display: inline-block;

}

#cat-nav a:link { color: #fff; text-decoration: none; padding: 10px; display: inline-block;}
#cat-nav a:hover { color: #fff; text-decoration: none; }
#cat-nav a:visited { color: #fff; text-decoration: none; }
#cat-nav a:active { color: #fff; text-decoration: none; }

ul .drop-menu-items  {
    position: absolute;
    display: none;
    background: #fff url(drop-menu-bg.png) bottom repeat-x;
    -webkit-box-shadow: 0px 5px 5px #8f8f8f;
    -moz-box-shadow:    0px 5px 5px #8f8f8f);
    box-shadow:         0px 5px 5px #8f8f8f;
    border-radius: 0 10px 10px 10px;

}

ul.drop-menu-items  li{
    background: url(drop-menu-bullet.png) no-repeat 5px center;
    float: none;
    position: relative;
    color: #000;
    padding: 5px 15px;
    margin: 0 10px; 
    border-bottom: 1px dotted #cecece;
}

ul.drop-menu-items  li:hover {
    background: #f0f0f0 url(drop-menu-bullet.png) no-repeat  5px center;
} 

.drop-menu-parent:hover .drop-menu-items {
    display: block;
    z-index: 1000;
}

#cat-nav > li:hover a{
    color:#000;
}

ul#cat-nav li   a:hover {
    background: #fff;
    border-radius: 5px;
    /* padding-top: 5px; */

}

#cat-nav .drop-menu-items li a {
    color: #000;
}


#cat-nav .drop-menu-items li:hover a {
    text-decoration: underline;
    background: none;
    color:     #000;
}

.drop-menu-parent:hover {
    background: #fff;
    background-color: #fff;
    border-radius: 5px 5px 0 0;

}
​

<强>解释

我添加的内容是:

#cat-nav > li:hover a{
    color:#000;
}

'&GT;'表示直接的孩子...所以#cat-nav的直接孩子在悬停时会为标签指定黑色。

你也可以写:#cat-nav .drop-menu-parent:hover > a { color:#000; } 它也会起作用......事实上,第二位是更好的选择。

答案 1 :(得分:2)

添加:

ul#cat-nav li:hover a{
    color: #000;
}

答案 2 :(得分:0)

不确定我是否理解你(?),但这是一个例子。你是这个意思吗? jsFiddle

nav {
    background: #000;
}

#cat-nav {
    text-align: center;
    background-color: #0f0609, #0f0609;
    background-image: url(cat-nav-bg.jpg), url(cat-nav-bg-bottom.jpg);
    background-repeat: no-repeat, repeat-y;
    margin-top: -8px;
    display: block;
}

#cat-nav:after {
    content: " ";
    display: block;
    clear: both;

    width: 81%;
    padding-top: 8px;
    margin: 0 auto;
    background: url(teeth.png) repeat-x;
}

ul#cat-nav > li {
    text-align: center;
    display: inline-block;

}

#cat-nav a:link { color: #fff; text-decoration: none; padding: 10px; display: inline-block;}
#cat-nav a:hover { color: #fff; text-decoration: none; }
#cat-nav a:visited { color: #fff; text-decoration: none; }
#cat-nav a:active { color: #fff; text-decoration: none; }

ul .drop-menu-items  {
    position: absolute;
    display: none;
    background: #000 url(drop-menu-bg.png) bottom repeat-x;
    -webkit-box-shadow: 0px 5px 5px #8f8f8f;
    -moz-box-shadow:    0px 5px 5px #8f8f8f);
    box-shadow:         0px 5px 5px #8f8f8f;
    border-radius: 0 10px 10px 10px;

}

ul.drop-menu-items  li{
    background: url(drop-menu-bullet.png) no-repeat 5px center;
    float: none;
    position: relative;
    color: #000;
    padding: 5px 15px;
    margin: 0 10px; 
    border-bottom: 1px dotted #cecece;
}

ul.drop-menu-items  li:hover {
    background: #000 url(drop-menu-bullet.png) no-repeat  5px center;
} 

.drop-menu-parent:hover .drop-menu-items {
    display: block;
    z-index: 1000;
}

ul#cat-nav li   a:hover {
    background: #000;
    border-radius: 5px;
    color: #fff;
    /* padding-top: 5px; */

}

#cat-nav .drop-menu-items li a {
    color:     #fff;
}


#cat-nav .drop-menu-items li:hover a {
    text-decoration: underline;
    background: none;
    color:     #fff;
}

.drop-menu-parent:hover {
    background: #000;
    background-color: #000;
    border-radius: 5px 5px 0 0;

}