CSS菜单问题:当父菜单项处于活动(当前)页面时,子菜单显示为活动状态

时间:2013-01-14 00:54:18

标签: css drop-down-menu submenu

我很困惑!这是我正在开发的网站:Cancer Support site

我似乎有两个问题......我花了几个小时试图找出解决方案..所以我真的希望有人可以提供帮助。

问题1 :我使用ID = navactive来保持父菜单项处于活动状态,如果它是当前正在显示的页面。这样可行。但是,我编写CSS的方式,通过使子菜单项首次出现在ID = navactive parent的mousever上时,它引入了一个错误。相反,子菜单项最初应显示为非活动状态,直到它们实际被鼠标悬停为止。在网站上,您可以看到“主页”页面是父菜单上的活动页面。如果将鼠标悬停在它上面,则会出现两个子子菜单..但它们显示为活动状态。它们应显示为非活动状态 - 与鼠标悬停在“我们的工作”父菜单项上时的子菜单相同。

问题2 :我的孙子项子菜单都没有出现..在“主页”和“我们的工作”父菜单项下,我有以下配置:

Top level menu (parent)
    Submenu 1 (child 1)
        Submenu 2 (grandchild 1)
    Submenu 3 (child 2)

相反,它出现在这样的页面上,其中孙子1成为孩子2,并且没有孙子可见(原始孩子2 [子菜单3]缺失):

Top level menu (parent)
    Submenu 1 (child 1)
    Submenu 2 (child 2)

任何建议都将不胜感激!!! 谢谢!

为了您的方便..这是我的导航菜单部分的css代码:

#navcontainer 
{
    width: 711px;
    height: 25px;
    text-align: center;
    margin: 0px auto; /*Center container on page*/
    clear: both;
    background-color: #129F9F;
    border: 3px solid #FFFFFF;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    -o-border-radius: 5px;
    -ms-border-radius: 5px;
    border-radius: 5px; 

    /* IE10 Consumer Preview */ 
    background-image: -ms-linear-gradient(top, #16ACAC 0%, #0D6F6F 100%);

    /* Mozilla Firefox */ 
    background-image: -moz-linear-gradient(top, #16ACAC 0%, #0D6F6F 100%);

    /* Opera */ 
    background-image: -o-linear-gradient(top, #16ACAC 0%, #0D6F6F 100%);

    /* Webkit (Safari/Chrome 10) */ 
    background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0, #16ACAC), color-stop(1, #0D6F6F));

    /* Webkit (Chrome 11+) */ 
    background-image: -webkit-linear-gradient(top, #16ACAC 0%, #0D6F6F 100%);

    /* W3C Markup, IE10 Release Preview */ 
    background-image: linear-gradient(to bottom, #16ACAC 0%, #0D6F6F 100%); 
}
.main_menu
{
    height: 24px;
    line-height: 24px;
    font-size: 12px;
    position: relative;
}
.main_menu ul
{
    padding: 0px 0px 0px 35px;  /* padding on left to get nav menu to center.. since it has a float left to make it display properly*/
    list-style: none;
}
.main_menu ul li
{
    padding: 0;
    margin: 0;
    border-right: 2px solid #129F9F;
    float: left;
}
.main_menu ul li.navcontact  /* to stop right border at end of nav line */
{
    padding: 0;
    margin: 0;
    border-right: none;
    float: left;
}
.main_menu ul li a
{
    color: #FFF;
    display: block;
    text-decoration: none;
    padding: 0 15px;
}
.main_menu ul > li:hover > a, #navactive a:link, #navactive a:visited, #navactive > ul > li > a:hover 
{
    text-decoration: none;
    color: #EAA339;
    /* IE10 Consumer Preview */ 
    background-image: -ms-linear-gradient(top, #0D6F6F 0%, #16ACAC 100%);

    /* Mozilla Firefox */ 
    background-image: -moz-linear-gradient(top, #0D6F6F 0%, #16ACAC 100%);

    /* Opera */ 
    background-image: -o-linear-gradient(top, #0D6F6F 0%, #16ACAC 100%);

    /* Webkit (Safari/Chrome 10) */ 
    background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0, #0D6F6F), color-stop(1, #16ACAC));

    /* Webkit (Chrome 11+) */ 
    background-image: -webkit-linear-gradient(top, #0D6F6F 0%, #16ACAC 100%);

    /* W3C Markup, IE10 Release Preview */ 
    background-image: linear-gradient(to bottom, #0D6F6F 0%, #16ACAC 100%); 
}
.main_menu ul li ul
{
    display: none;
    width: auto;
    position: absolute;
    padding: 0px;
    margin: 0px;
}
.main_menu ul li:hover ul
{
    display: block;
    position: absolute;
    margin: 0;
    padding: 0;
}
.main_menu ul li:hover li
{
    float: none;
    list-style: none;
    margin: 0px;
}
.main_menu ul li:hover li
{
    font-size: 12px;
    height: 24px;
    background: #54C4C4;
    /* border-top: 1px solid #129F9F; */ 
    border: 1px solid #FFFFFF;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    -o-border-radius: 5px;
    -ms-border-radius: 5px;
    border-radius: 5px; 
}
.main_menu ul li:hover li a
{
    font-size: 11px;
    color: #fff;
    padding: 0px;
    display: block;
    width: 150px;
}
.main_menu ul li li a:hover
{
    font-size: 11px;
    height: 24px;
    color:#EAA339;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    -o-border-radius: 5px;
    -ms-border-radius: 5px;
    border-radius: 5px; 
}

这是HTML代码:

    <div id="navcontainer">
  <div class="main_menu">  
      <ul>
        <li id="navactive"><a href="index.asp" title="Cancer Support for Families Foundation is a community funded, community focused cancer charity.">Home</a>
          <ul>
            <li><a href="CSFF_presentation.pdf" title="Presentation about CSS in PDF format">submenu 1</a>
              <ul>
                <li><a href="CSFF_presentation.pdf" title="Presentation about CSS in PDF format">submenu 2</a></li>
              </ul>
            </li>
            <li><a href="CSFF_presentation.pdf" title="Presentation about CSS in PDF format">submenu 3</a></li>
          </ul>
        </li>
        <li><a href="what_is_csff.asp" title="Supporting families in financial need who are living with cancer.">What is CSFF</a>
          <ul>
            <li><a href="CSFF_presentation.pdf" title="Presentation about CSS in PDF format">submenu 4</a></li>
          </ul>
        </li>
        <li><a href="make_a_donation.asp" title="CSFF supporters can fund the charitable support programs, community experiences and fundraising events.">Make a Donation</a></li>
        <li><a href="our_work.asp" title="Individuals and families battling cancer need more financial support. CSFF can help.">Our Work</a>
          <ul>
            <li><a href="CSFF_presentation.pdf" title="Presentation about CSS in PDF format">submenu 5</a>
              <ul>
                <li><a href="CSFF_presentation.pdf" title="Presentation about CSS in PDF format">submenu 6</a></li>
              </ul>
            </li>
            <li><a href="CSFF_presentation.pdf" title="Presentation about CSS in PDF format">submenu 7</a></li>
          </ul>
        </li>
        <li><a href="our_events.asp" title="Local communities come together in a relaxed family friendly environment to support those living with cancer.">Events Outline</a></li>
        <li class="navcontact"><a href="contact_csff.asp" title="Contact CSFF to help those in need.">Contact Us</a></li>
      </ul>
  </div>
</div>

2 个答案:

答案 0 :(得分:0)

您遇到此问题的原因是由于您的CSS以及构建HTML代码的方式。这是你的问题 -

.main_menu ul > li:hover > a, #navactive a:link, #navactive a:visited, #navactive > ul > li > a:hover 
{
 STYLES
}

只需将上面CSS代码的“#navactive a:link”部分更改为处理活动链接的单独类。目前,“navactive”div中的任何链接都将继承此类中给出的样式,这就是子菜单文本显示为黄色而不是白色的原因。就像我说的那样将上面的CSS代码更改为一个单独的类,例如 - “#navactive .current”

然后将HTML代码更改为:

<div id="navcontainer">
 <div class="main_menu">  
  <ul>
    <li id="navactive"><a class="current" href="index.asp" title="Cancer Support for
Families Foundation is a community funded, community focused cancer charity.">Home</a>

应该这样做!显然我无法测试它,但如果你有任何问题,请告诉我。还有一个提示 - 使用Chrome'Inspect Element'功能在面对这些问题时非常有用!

答案 1 :(得分:0)

问题1 在css中:

.main_menu ul > li:hover > a, #navactive a:link, #navactive a:visited, #navactive > ul > li > a:hover

你应该放一个&gt;在#navactive a:visited和#navactive a:link中,它看起来像这样:

.main_menu ul > li:hover > a, #navactive > a:link, #navactive > a:visited, #navactive > ul > li > a:hover

“&gt;”意味着你只引导子元素