简体   繁体   English

悬停任何子项时更改CSS下拉菜单的颜色

[英]Change CSS dropdown menu color when any child is hovered

I have a basic CSS dropdown menu that looks like this: http://jsfiddle.net/qfTt3/ (same code below) 我有一个基本的CSS下拉菜单,如下所示: http : //jsfiddle.net/qfTt3/ (以下相同代码)

HTML 的HTML

<ul id="main-navigation">
                <li class="active"><a href='#'>Plans</a>
                    <ul>
                    <li><a href='#'>Sub Link 1</a></li>
                    <li><a href='#'>Sub Link 2</a></li>
                    </ul>
                </li>
                <li><a href='#'>How it Works</a></li>
                <li><a href='#'>About</a>
                    <ul>
                    <li><a href='#'>Sub Link 1</a></li>
                    <li><a href='#'>Sub Link 2</a></li>
                    <li><a href='#'>Sub Link 3</a></li>
                    <li><a href='#'>Sub Link 4</a></li>
                    </ul>
                </li>
                <li><a href='#'>Testimonials</a>
                    <ul>
                    <li><a href='#'>Sub Link 1</a></li>
                    <li><a href='#'>Sub Link 2</a></li>
                    <li><a href='#'>Sub Link 3</a></li>
                    <li><a href='#'>Sub Link 4</a></li>
                    </ul>
                </li>
                <li><a href='#'>FAQ</a></li>
                <li><a href='#'>Contact</a></li>
            </ul>

CSS 的CSS

#main-navigation {
    background: #FFF;
    padding: 0;
    margin: 0;
    list-style-type: none;
    height: 100px;
    float: right;
    font-size: 18px;
}

#main-navigation li {
    float: left;
}

#main-navigation>li {
    line-height: 100px;
    background-color: #FFF;
    margin-left: 10px;
}

#main-navigation>li>ul>li {
    line-height: 30px;
    background: #FFF;
    margin-left: 0px;
    border-bottom: 1px solid #999;
    position: relative;
    z-index: 100;
}

#main-navigation li a {
    padding: 0px 12px;
    display: block;
    text-decoration: none;
    color: #6d6e71;
}

#main-navigation ul {
    position: absolute;
    left: -9999px;
    top: -9999px;
    list-style-type: none;
    margin: 0;
}

#main-navigation li:hover {
    position: relative;
    background: #10b794;
}

#main-navigation li a:hover {
    color: #FFF;
}

#main-navigation li:hover ul {
    left: 0px;
    top: 100px;
    background: #10b794;
    padding: 0px;
}

#main-navigation li:hover ul li a {
    padding: 5px;
    display: block;
    width: 168px;
    text-indent: 15px;
    background: #10b794;
}

#main-navigation li:hover ul li a:hover {
    color: #FFF;
}

#main-navigation li.active {
    border-bottom: 4px solid #10b794;
    height: 96px;
}

As you can see, the text color changes to white when and individual item is hovered over. 如您所见,当鼠标悬停在单个项目上时,文本颜色变为白色。 What I would like to do is have the text color of both the main <li> as well as the submenu items change to white if any part of that menu/submenu is hovered over. 我想做的是将主<li>以及子菜单项的文本颜色都更改为白色(如果该菜单/子菜单的任何部分都悬停了)。 If someone hovers over 'Plans' in the menu, all the submenu links should have white text as well. 如果有人将鼠标悬停在菜单中的“计划”上,则所有子菜单链接也应具有白色文本。 If this possible with CSS selectors alone or do I need to look into a JS solution? 如果仅使用CSS选择器可能实现此目的,还是需要研究JS解决方案?

You want to change: 您要更改:

#main-navigation li a:hover {
    color: #FFF;
}

to be: 成为:

#main-navigation li:hover > a {
    color: #FFF;
}

JSFiddle here. JSFiddle在这里。

Basically, you want the a element's color to change when you are hovered over the list item. 基本上,你想要的a ,当你悬停在列表项元素的颜色改变。 That way, when you hover over other submenu items, you're still hovering over the li containing the submenu. 这样,当您将鼠标悬停在其他子菜单项上时,仍将鼠标悬停在包含该子菜单的li

I use the child selector > so that the submenu item links are not affected when you're hovering over the main menu item link. 我使用子选择器>这样当您将鼠标悬停在主菜单项链接上时,子菜单项链接不会受到影响。


To target the Plans submenu link colors, you should apply the styling to a class to specifically target them. 要定位“计划”子菜单链接颜色,应将样式应用于类以专门定位它们。 Since you already have a class specifically on Plans ( .active ), I'll just use that for demonstration purposes. 由于您已经有一个专门针对Plans( .active )的类,因此我仅将其用于演示目的。

CSS: CSS:

#main-navigation li:hover > a, #main-navigation .active:hover a {
    color: #FFF;
}

JSFiddle here. JSFiddle在这里。

I get rid of the child selector when targeting .active so that it makes all child a elements white when hovering over the main link. 当定位.active时,我.active了子选择器,这样当悬停在主链接上时,它将所有子元素a白色。

You must add this to your css 您必须将此添加到您的CSS

#main-navigation > li:hover > ul > li > a {
    color: #FFF;
}

http://jsfiddle.net/sijav/qfTt3/1/ http://jsfiddle.net/sijav/qfTt3/1/

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

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