html - 垂直菜单,鼠标悬停时子菜单会飞出

标签 html css submenu

我正在尝试创建一个带有弹出菜单的垂直菜单。这是一个带有子菜单的垂直菜单。以下代码有什么问题?

<html>
    <head>
        <title>Untitled Document</title>
        <style type="text/css">
            #navmenu ul ul li a {
                border:1px solid #888888; border-bottom: none; font-size:12pt; line-height: 1.6em; color:#303030; background-color:#a5a5a5; background-image:none;
            }
            #navmenu {
                width: 150px; /* set width of menu */
            }  
            #navmenu ul {
                list-style-type:none; margin:0px;   padding:0px;
            }
            #navmenu a {
                text-decoration:none; border: 1px solid #303030; width:170px;   display:block;   text-align:center;   font-size:14pt;   line-height:2em; background:url(Button_top.gif) repeat-x left;   font-family:Arial, Helvetica, sans-serif; color:white;
            }
            #navmenu a:hover {
                color: #a00;
                /* red text color on hover */
                background: #fff;
                /* white bgcolor on hover */
            }
            #navmenu li {
                /* make the list elements a containing block for the nested lists */
                position: relative;
            }
            #navmenu ul ul {
                position: absolute; top: 0; left: 100%;
                /* to position them to the right of their containing block */
                width: 100%;
                /* width is based on the containing block */
                }  
            #navmenu li {
                /* make the list elements a containing block for the nested lists */
                position: relative;
            }
            #navmenu ul ul {
                display: none;
            }
            #navmenu ul li:hover ul {
                display:block;
            }
        </style>
    </head>
    <body>
        <div id="navmenu">
            <ul>
                <li>
                    <a href="#">Home</a>
                </li>
                <li>
                    <a href="#">Blog</a>
                </li>
                <ul>
                    <li>
                        <a href="#">Blog 1</a>
                    </li>
                    <li>
                        <a href="#">Blog 2</a>
                    </li>
                </ul>
                <li>
                    <a href="#">Websites</a>
                </li>
                <ul>
                    <li>
                        <a href="#">Websites 1</a>
                    </li>
                    <li>
                        <a href="#">Websites 2</a>
                    </li>
                </ul>
                <li>
                    <a href="#">Photos</a>
                </li>
            </ul>
        </div>
    </body>
</html>

http://jsfiddle.net/9bHkj/1/

最佳答案

您可能需要重新审视您构建菜单的方式。例如:

            <li>
                <a href="#">Blog</a>
            </li>
            <ul>
                <li>
                    <a href="#">Blog 1</a>
                </li>
                <li>
                    <a href="#">Blog 2</a>
                </li>
            </ul>

应该是 Blog带有两个子菜单的菜单 Blog 1Blog 2 .但是然后 <ul>因为子菜单应该在 <li>菜单的而不是单独的:

   <li>
      <a href="#">Blog</a>
      <!-- The <li> does not end here -->
      <ul>
         <li>
            <a href="#">Blog 1</a>
         </li>
         <li>
            <a href="#">Blog 2</a>
         </li>
      </ul>
   </li>  <!-- end tag for the blog <li>, now enclosing the submenu also -->

执行此操作后,对于其他子菜单,您也将获得飞行子菜单。您现在可以算出位置、颜色等。

关于html - 垂直菜单,鼠标悬停时子菜单会飞出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6894048/

相关文章:

javascript - 带 SVG 的 Tabby JS 防止滚动

html - CSS 中的媒体查询无法正常工作

Java:创建滚动子菜单

c# - HTML-5 data-* 属性的使用在 ASP.NET MVC 4(测试版)中被破坏了吗?

html - 如何删除页面顶部的边距

html - 悬停期间的淡入淡出效果 - border-radius 不起作用

html - CSS 选择器排除 DIV 元素的内容?

html - 如何使我的 kendo treeview div 可滚动且不展开

perl - 如何在 Perl 中构建一个简单的菜单?

javascript - jquery 保持项目可见,直到选择另一个项目