1

我正在菜单上为我的博客写一篇文章。我正在尝试创建一个圆形下拉菜单。当所有浏览器上有两个或更多子菜单时,它工作正常。但是,如果父菜单只有一个子菜单,则它会显示在其预期位置下方。

这是 jsfiddle 中以下代码的工作副本:http: //jsfiddle.net/avdhut/LDmM8/5/

我知道问题出在#menucontainer ul li:hover > ul选择器上,它有边距。但是,如果我删除具有 2 个或更多子菜单的顶部边距菜单,则会上升并显示在其预期位置之上。请帮忙,因为我找不到适合这个问题的解决方案。

以下是 HTML 代码:

<div id="menucontainer">
  <ul id="hmenu">
    <li><a href="http://techisquest.blogspot.com/">Home</a>
      <ul>
        <li><a href="http://techisquest.blogspot.com/">Test 1</a></li>
        <li><a href="http://techisquest.blogspot.com/">Test 2</a></li>
        <li><a href="http://techisquest.blogspot.com/">Test 3</a></li>
        <li><a href="http://techisquest.blogspot.com/">Test 3</a></li>
      </ul>
    </li>
    <li><a href="http://techisquest.blogspot.com/">About</a>
      <ul>
        <li><a href="http://techisquest.blogspot.com/">Test 1</a></li>
        <li><a href="http://techisquest.blogspot.com/">Test 2</a></li>
      </ul>
    </li>
    <li><a href="http://techisquest.blogspot.com/">Contact Us</a>
      <ul>
        <li><a href="http://techisquest.blogspot.com/">Test 1</a></li>
      </ul>
    </li>
    <li><a href="http://techisquest.blogspot.com/">Info</a>
      <ul>
        <li><a href="http://techisquest.blogspot.com/">Test 2</a></li>
        <li><a href="http://techisquest.blogspot.com/">Test 3</a></li>
      </ul>
    </li>
  </ul>
</div>

以下是 CSS 代码:

 #menucontainer {
    margin: 0px;
    padding: 0px;
    width: 100%;
    height: 90px;
}
/* Following selectors will define the color and the border radius for the menu*/
 #menucontainer ul, #menucontainer ul li ul {
    list-style: none;
    margin: 0px;
    padding: 0px;
}
/* Following selector will define the style for individual menu or li tags. The following style gives them a look when the menus are not selected.
note that the position proerty is set to relative and dispaly is inline.
*/
 #menucontainer ul li {
    list-style: none;
    margin: 10px;
    padding: 0px;
    background-color:#2CDF7B;
    position: relative;
    display: inline;
    text-align: center;
    /*Setting the height and width for the menu*/
    width: 90px;
    cursor:pointer;
    height: 90px;
    /*Setting the border-radius for the menu
    various prefix are added so it is supported in older version of
    browsers. For IE supported from IE-9+
    IMPORTANT: to keep the border radius height and width same to get circle.
    */
    -moz-border-radius: 90px;
    -webkit-border-radius: 90px;
    -khtml-border-radius: 90px;
    border-radius: 90px;
    /*Adding the box shadow to the menu*/
    box-shadow: 0 0 10px black;
    -moz-box-shadow: 0 0 10px black;
    -webkit-box-shadow: 0 0 10px black;
    float: left;
}
/* Style for the anchor tag defined in the menu. 
*/
 #menucontainer ul li a {
    margin: 10px;
    padding: 10px;
    background-color:transparent;
    display: block;
    text-decoration: none;
    text-align: center;
    font-family:"Times New Roman", "Arabic";
    font-size: 18px;
    color: #E3E3C0;
}
/* The hover effect is defined using the below selectors
For the opacity is used to create a semi-transperant look as a hover effect
*/
 #menucontainer ul li:hover {
    list-style: none;
    margin: 10px;
    padding: 0px;
    opacity: 0.7;
    background-color:#2CDF7B;
    position: relative;
    display: block;
    width: 90px;
    text-align: center;
    cursor:pointer;
    height: 90px;
    /*Setting the border-radius for the menu
    various prefix are added so it is supported in older version of
    browsers. For IE supported from IE-9+
    IMPORTANT: to keep the border radius height and width same to get circle.
    */
    -moz-border-radius: 90px;
    -webkit-border-radius: 90px;
    -khtml-border-radius: 90px;
    border-radius: 90px;
    -moz-box-shadow: inset 0 0 10px black;
    box-shadow: inset 0 0 10px black;
    -webkit-box-shadow: inset 0 0 10px black;
}
#menucontainer ul li:hover a {
    margin: 10px;
    padding: 10px;
    background-color:transparent;
    display: block;
    text-decoration: none;
    text-align: center;
    font-family:"Times New Roman", "Arabic";
    font-size: 18px;
    color: white;
}
/*******************************************************/

/*******************************************************/

/*Sub menu*/

/*******************************************************/
 #menucontainer ul li > ul {
    display : none;
    overflow: hidden;
    position: relative;
    border-bottom: solid 1px;
    border: 0px;
    height: 0px;
    width: 100%;
}
#menucontainer ul li:hover > ul {
    z-index: 10;
    float: none;
    left: -15px;
    margin: 40px 10px 10px 10px;
    height: auto;
    padding: 0px 5px 0px 0px;
    display:block;
    width: 100px;
}
#menucontainer ul li:hover > ul li, #menucontainer ul li a:hover > ul li {
    list-style: none;
    margin: 10px;
    padding: 0px;
    background-color:#2CDF00;
    position: relative;
    display: inline;
    text-align: center;
    /*Setting the height and width for the menu*/
    width: 90px;
    cursor:pointer;
    height: 90px;
    /*Setting the border-radius for the menu
    various prefix are added so it is supported in older version of
    browsers. For IE supported from IE-9+
    IMPORTANT: to keep the border radius height and width same to get circle.
    */
    -moz-border-radius: 90px;
    -webkit-border-radius: 90px;
    -khtml-border-radius: 90px;
    border-radius: 90px;
    /*Adding the box shadow to the menu*/
    box-shadow: 0 0 10px black;
    -moz-box-shadow: 0 0 10px black;
    -webkit-box-shadow: 0 0 10px black;
    float: left;
}
/*******************************************************/
 #menucontainer ul li:hover > ul li:after, #menucontainer ul li a:hover > ul li:after {
    content:" ";
    border: solid transparent;
    margin: 0px;
    padding: 0px;
    position: absolute;
    pointer-events: none;
    border-color: rgba(44, 223, 123, 0);
    border-top-color: #2CDF00;
    top: 98%;
    left: 50%;
    margin-left: -10px;
    border-width:10px;
}
#menucontainer ul li:hover > ul li:before, #menucontainer ul li a:hover > ul li:before {
    content:" ";
    border: solid transparent;
    margin: 0px;
    padding: 0px;
    position: absolute;
    pointer-events: none;
    border-color: rgba(44, 223, 123, 0);
    border-bottom-color: #2CDF00;
    bottom: 98%;
    left: 50%;
    margin-left: -10px;
    border-width:10px;
}
/*******************************************************/
4

2 回答 2

2

只需对 css 进行一些更改,您就完成了:-

#menucontainer ul li > ul {
    display : none;
    overflow: hidden;
    position: absolute; //implement absolute
    border-bottom: solid 1px;
    border: 0px;
    height: 0px;
    width: 100%;
    top:90px; //add top
}
#menucontainer ul li:hover > ul {
    z-index: 10;
    float: none;
    left: -15px;
    margin: 5px; //change it to this
    height: auto;
    padding: 0px 5px 0px 0px;
    display:block;
    width: 100px;
}

工作小提琴:- http://jsfiddle.net/LDmM8/7/

于 2013-09-02T05:49:13.707 回答
1

这与您在该下拉菜单中只有一项这一事实无关。您可以尝试添加更多,看看它仍在发生。真正的原因是“联系我们”按钮占用两行而不是像其他按钮那样占用一行。

这个问题通过设置一个固定的高度来解决anchors

jsFiddle 演示

#menucontainer ul li a {
    height:33px;
}
于 2013-09-02T05:23:51.897 回答