第三级中心菜单下拉导航问题

时间:2014-09-25 19:20:10

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

我有一个居中的第三级导航,在第二级悬停时显示。我错过了什么,直到将它隐藏起来直到将其隐藏起来?

P.S。 - 我知道代码会更干净>正如我在网上看到的那样,但不知道如何清理它,所以我希望代码清晰可见。

谢谢!

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>

2 个答案:

答案 0 :(得分:1)

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

&# 13;
&#13;
#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;
}
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

CSS中的

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

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

我今天正在玩这样的事情,请参阅http://codepen.io/ckuijjer/pen/huyxn我的例子。我试图使用大多数类,几乎没有任何元素样式。