【问题标题】:3rd Level Centered Menu Dropdown Navigation Issue第三级居中菜单下拉导航问题
【发布时间】:2014-09-25 19:20:10
【问题描述】:

我有一个居中的 3 级导航,它在 2 级悬停时显示。我缺少什么来隐藏它直到直接悬停在它上面?

附: - 正如我在网上看到的那样,我知道带有 > 的代码会更干净,但不知道如何清理它,所以我希望代码是清晰的。

谢谢!

JS 小提琴:DEMO

#centeredmenu {
    clear:both;
    float:left;
    margin:0 0 30px 0;
    padding:0;
    border-bottom:1px solid #000; /* black line below menu */
    width:100%;
    font-family:Arial, Helvetica, sans-serif; /* Menu font */
    z-index:1000; /* This makes the dropdown menus appear above the page content below */
    position:relative;
    background-color: #000;
}

/* Top menu items */
#centeredmenu ul {
    margin:0;
    padding:0;
    list-style:none;
    float:right;
    position:relative;
    right:50%;
}
#centeredmenu ul li {
   margin:0 0 0 1px;
   padding:0;
   float:left;
   position:relative;
   left:50%;
   top:1px;
}
#centeredmenu ul li a {
    display: block;
    margin: 0;
    padding: 10px 20px;
    font-size: 1em;
    line-height: 1em;
    text-decoration: none;
    color: #fff;
    font-weight: bold;
    border-bottom: 1px solid #000;
}
#centeredmenu ul li a:hover {
    background: #a3c2df; /* Top menu items background color */
    color: #fff;
    border-bottom: 1px solid #03f;
}
#centeredmenu ul li:hover a { 
    background: #a3c2df; /* Top menu items background color */
    color: #000;
    border-bottom: 1px solid #03f;
}

/* 2nd Level Items */
#centeredmenu ul ul {
    display:none; /* Submenus are hidden by default */
    position:absolute;
    left:0;
    right:auto; /* Resets the right:50% on the parent ul */
    width:12em; /* Width of the drop-down menus */
}
#centeredmenu ul ul li {
   left:auto;  /*Resets the left:50% on the parent li */
   margin:0; /* Resets the 1px margin from the top menu */
   clear:left;
   width:100%;
}

/* 3rd Level Items */
#centeredmenu ul ul ul {
    display:none; /* Submenus are hidden by default */
    position:absolute;
    top:0; 
    left:155px;
    right:auto; /* Resets the right:50% on the parent ul */
    width:12em; /* Width of the drop-down menus */
}
#centeredmenu ul ul ul li {
   left:auto;  /* Resets the left:50% on the parent li */
   margin:0; /* Resets the 1px margin from the top menu */
   clear:left;
   width:100%;
}

#centeredmenu ul ul li a,
#centeredmenu ul li.active li a,
#centeredmenu ul li:hover ul li a { 
    font-size:0.9em;
    font-weight:normal; /* Resets the bold set for the top level menu items */
    background:#eee;
    color:#444;
    line-height:1.4em; /* Overwrite line-height value from top menu */
    border-bottom:1px solid #ddd; /* Submenu item horizontal lines */
}
#centeredmenu ul ul li a:hover,
#centeredmenu ul li.active ul li a:hover,
#centeredmenu ul li:hover ul li a:hover { 
    background: #a3c2df; /* Submenu items background color */
    color:#000;  /* Submenu items hover color */
}

/* Flip the last 2nd menu so it stays within the page */
#centeredmenu ul ul.last {
   left:auto; /* Resets left:0; value */
   right:0; /* Set right value instead */
}

/* Make the 2nd menus appear on hover */
#centeredmenu ul li:hover ul { 
   display:block; /* Show the submenus */
}

/* Make the 3rd menus appear on hover */
#centeredmenu ul li:hover ul ul{ 
   display:block; /* Show the submenus */
}
<div id="centeredmenu">
   <ul>
      <li><a href="#">Home</a></li> 
       <li><a href="#">Documents</a>
         <ul> 
            <li><a href="#">Reading</a></li>  
            <li><a href="#">Writing</a>
                <ul>        
                    <li><a href="#">Excerpt 1</a></li>
                    <li><a href="#">Excerpt 2</a></li>
                    <li><a href="#">Excerpt 3</a></li>
                    <li><a href="#">Excerpt 4</a></li>

                </ul>
            </li>

         </ul>
      </li>
   </ul>
</div>

【问题讨论】:

    标签: html css drop-down-menu navigation center


    【解决方案1】:

    #centeredmenu ul li:hover ul 表示 all ul 悬停元素下方的元素(无论深度如何)!这反过来意味着当您将鼠标悬停在li 上时,该li 元素的每个ul 子元素都将具有关联的规则。 #centeredmenu ul li:hover &gt; ul 表示第一个后代(> - 仅限第一个后代)!这可以防止悬停传播到预期水平以下。 "#centeredmenu ul li:hover ul ul" 也有同样的问题,只是开始向下传播 2 级并一直到结束。

    #centeredmenu {
      clear: both;
      float: left;
      margin: 0 0 30px 0;
      padding: 0;
      border-bottom: 1px solid #000;
      /* black line below menu */
      width: 100%;
      font-family: Arial, Helvetica, sans-serif;
      /* Menu font */
      z-index: 1000;
      /* This makes the dropdown menus appear above the page content below */
      position: relative;
      background-color: #000;
    }
    /* Top menu items */
    
    #centeredmenu ul {
      margin: 0;
      padding: 0;
      list-style: none;
      float: right;
      position: relative;
      right: 50%;
    }
    #centeredmenu ul li {
      margin: 0 0 0 1px;
      padding: 0;
      float: left;
      position: relative;
      left: 50%;
      top: 1px;
    }
    #centeredmenu ul li a {
      display: block;
      margin: 0;
      padding: 10px 20px;
      font-size: 1em;
      line-height: 1em;
      /* [disabled]background: #ddd; */
      text-decoration: none;
      color: #fff;
      font-weight: bold;
      border-bottom: 1px solid #000;
    }
    #centeredmenu ul li a:hover {
      background: #a3c2df;
      /* Top menu items background colour */
      color: #fff;
      border-bottom: 1px solid #03f;
    }
    #centeredmenu ul li:hover a {
      background: #a3c2df;
      /* Top menu items background colour */
      color: #000;
      border-bottom: 1px solid #03f;
    }
    /* 2nd Level Items */
    
    #centeredmenu ul ul {
      display: none;
      /* Sub menus are hiden by default */
      position: absolute;
      left: 0;
      right: auto;
      /*resets the right:50% on the parent ul */
      width: 12em;
      /* width of the drop-down menus */
    }
    #centeredmenu ul ul li {
      left: auto;
      /*resets the left:50% on the parent li */
      margin: 0;
      /* Reset the 1px margin from the top menu */
      clear: left;
      width: 100%;
    }
    /* 3rd Level Items */
    
    #centeredmenu ul ul ul {
      display: none;
      /* Sub menus are hiden by default */
      position: absolute;
      top: 0;
      left: 155px;
      right: auto;
      /*resets the right:50% on the parent ul */
      width: 12em;
      /* width of the drop-down menus */
    }
    #centeredmenu ul ul ul li {
      left: auto;
      /*resets the left:50% on the parent li */
      margin: 0;
      /* Reset the 1px margin from the top menu */
      clear: left;
      width: 100%;
    }
    #centeredmenu ul ul li a,
    #centeredmenu ul li.active li a,
    #centeredmenu ul li:hover ul li a {
      font-size: 0.9em;
      font-weight: normal;
      /* resets the bold set for the top level menu items */
      background: #eee;
      color: #444;
      line-height: 1.4em;
      /* overwrite line-height value from top menu */
      border-bottom: 1px solid #ddd;
      /* sub menu item horizontal lines */
    }
    #centeredmenu ul ul li a:hover,
    #centeredmenu ul li.active ul li a:hover,
    #centeredmenu ul li:hover ul li a:hover {
      background: #a3c2df;
      /* Sub menu items background colour */
      color: #000;
      /* Sub menu items hover colour */
    }
    /* Flip the last 2nd menu so it stays within the page */
    
    #centeredmenu ul ul.last {
      left: auto;
      /* reset left:0; value */
      right: 0;
      /* Set right value instead */
    }
    /* Make the 2nd menus appear on hover */
    
    #centeredmenu ul li:hover > ul {
      display: block;
      /* Show the sub menus */
    }
    #centeredmenu ul li ul li ul {
      display: none;
    }
    #centeredmenu ul li ul li:hover ul {
      display: block;
    }
    <div id="centeredmenu">
      <ul>
        <li><a href="#">Home</a>
        </li>
        <li><a href="#">Documents</a>
          <ul>
            <li><a href="#">Reading</a>
            </li>
            <li><a href="#">Writing</a>
              <ul>
                <li><a href="#">Excerpt 1</a>
                </li>
                <li><a href="#">Excerpt 2</a>
                </li>
                <li><a href="#">Excerpt 3</a>
                </li>
                <li><a href="#">Excerpt 4</a>
                </li>
    
              </ul>
            </li>
    
          </ul>
        </li>
      </ul>
    </div>

    【讨论】:

    • @emmanuel 你是对的,我很抱歉。只是现在有点暴躁。 ('在此处输入代码'是因为它不允许我发布小提琴链接' - 最终可能是个好主意)。
    • 谢谢@emmanuel,这正是我想要实现的。因此,如果我理解正确,ul li:hover >ul 用于所有 DD 菜单(第二、第三、第四等)的操作,但我仍然需要倒数第二个 CSS #centeredmenu ul li ul li ul 和#centeredmenu ul li ul li:悬停 ul 来控制何时。那是对的吗?或者也许最后一个也应该是 > ul?
    • 这是@RaduAndrei 的答案,我刚刚进行了编辑:)
    • 哦,谢谢!仍然无法正确获取此处的格式。严重的菜鸟。感谢@RaduAndrei 的帮助。
    【解决方案2】:

    #centeredmenu ul li:hover ul 在您的 CSS 中匹配菜单的两个级别。 li:hover ul 部分使浏览器搜索带有li:hover 祖先的ul 标记。这不一定是直系父母,也可以是祖父母、曾祖父母等。试着看看你是否理解为什么这对两个菜单级别都成立。 http://learn.shayhowe.com/html-css/getting-to-know-css/ 可能会让你对 CSS 选择器的工作原理有所了解

    快速解决方法是将#centeredmenu ul li:hover ul 更改为#centeredmenu ul li:hover &gt; ul 并删除您拥有的#centeredmenu ul li:hover ul ul

    我今天正在玩这样的东西,请参阅 http://codepen.io/ckuijjer/pen/huyxn 以获取我的示例。我尝试主要使用类,几乎没有任何元素样式。

    【讨论】:

    • 感谢@ckuijjer 的解释和说明。我一定会阅读该页面资源链接。仍然让我感到困惑的是所有各种 CSS 组合的放置,即 ul.last、li:hover 等。主要使用类甚至更高级 - codepen 很好!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2021-11-03
    相关资源
    最近更新 更多