转换CSS3导航菜单

时间:2013-07-29 15:21:29

标签: html css3 drop-down-menu menu

我发现了一个很棒的教程,我认为这是一个相当不错的菜单。我正在尝试将其转换为垂直下拉列表,类似于:http://nettuts.s3.amazonaws.com/699_nav/navCode/nav.htmlhttp://net.tutsplus.com/tutorials/html-css-techniques/how-to-build-a-kick-butt-css3-mega-drop-down-menu/?search_index=3

我只想让列表下拉而不是从侧面出来。我已经尝试了我认为可以解决的问题,并将li元素作为内联显示等等,但是我无法使其工作,无论我改变什么都没有效果或导致它成为混乱的混乱。

Fiddle

CSS

#thisNav{
    width: 250px;
    margin: 20px;
    background:#bada55;
}

#thisNav ul{
    list-style: none;
    margin: 0;
    padding: 0;
}

#thisNav ul li{
    /*child elements positioned absolutley will be relative to this*/
    position: relative;
    border-top: 1px solid #e9e9e9;

}

#thisNav a{
    color: ghostwhite;
    padding: 12px 0px;
    /*fill hori space*/
    display: block;
    text-decoration: none;
/*apply transition to background property, taking 1s to change it
*/
    transition:padding 1s, background 1s;
    -moz-transition:padding 1s, background 1s;
    -webkit-transition:padding 1s, background 1s;
    -o-transition:padding 1s, background 1s;

    font-family:tahoma;

    font-size:13px;
    text-transform:uppercase;
    padding-left:20px;
}

/*hover pseduo class*/
#thisNav a:hover{
    /*
    RGBA background for transparancy:
    last number(0.05) is the transparency
    */
    padding-left:35px;
    background: RGBA(255,255,255,0.05);
    color:#fff;
}

#thisNav ul li:hover ul{
    /*diplay when hovered*/
    display: block;

}

#thisNav ul ul{
    position: absolute;
    left: 250px;
    top: 0;
    border-top: 1px solid #e9e9e9;
    display: none;
    width: 304px;
}

#thisNav ul ul li{
    width: 150px;
    background: #f1f1f1;
    border: 1px solid #e9e9e9;
    border-top: 0;
    float:left;

}

#thisNav ul ul li a{
    color:#000000;
    font-size:12px;
    text-transform:none;
}

#thisNav ul ul li a:hover {
    color:#929292;
}

#thisNav span {
    width:12px;
    height:12px;
    background:#fff;
    display:inline-block;
    float:left;
    margin-top:3px;
    margin-right:10px;
    position:relative;
    transition:all 0.5s;
    -moz-transition:all 0.5s;
    -o-transition:all 0.5s;
    -webkit-transition:all 0.5s;
}
#thisNav a:hover span {
    background: #7d2c41;
    transform:rotate(90deg);
    -moz-transform:rotate(90deg);
    -webkit-transform:rotate(90deg);
}

/*Horizontal line*/
#thisNav span:before {
    content:"";
    width:12px;
    height:2px;
    background:#3a3b3b;
    position:absolute;
    left:0px;
    top:5px;
}
/*Vertical line*/
#thisNav span:after {
    content:"";
    width:2px;
    height:12px;
    background:#3a3b3b;
    position:absolute;
    left:5px;
    position:top;
}

HTML

<nav id = "thisNav">
        <ul>
            <li>
                <a href="#"><span></span>one</a>
                <ul>
                    <li><a href="#">sub1</a></li>
                    <li><a href="#">sub2</a></li>
                    <li><a href="#">sub3</a></li>
                </ul>
            </li>

            <li>
                <a href="#"><span></span>two</a>
                <ul>
                    <li><a href="#">sub1</a></li>
                    <li><a href="#">sub2</a></li>
                    <li><a href="#">sub3</a></li>
                </ul>
            </li>
    </ul>
</nav>

2 个答案:

答案 0 :(得分:1)

Changed some css in your fiddle

#thisNav ul ul{
    border-top: 1px solid #e9e9e9;
    display: none;
    width: 100%;
}

#thisNav ul ul li{
    width: 100%;
    background: #f1f1f1;
    border: 1px solid #e9e9e9;
    border-top: 0;
}

答案 1 :(得分:1)

试试这个:http://jsfiddle.net/BcYJj/7/

在主height上设置ul,以便绘制绿色背景(否则为0px高):

#thisNav ul{
    list-style: none;
    margin: 0;
    padding: 0;
    height: 40px;
}

浮动li,因此主li列为水平:

#thisNav ul li{
/*child elements positioned absolutley will be relative to this*/
position: relative;
border-top: 1px solid #e9e9e9;

float: left;

}

从子width移除ul,重新定位lefttop)并设置z-index。设置top / left使其显示在下方,而不是菜单项。由于可见性错误设置z-index

#thisNav ul ul{
    position: absolute;
    left: 0px;
    top: 40px;
    border-top: 1px solid #e9e9e9;
    display: none;
    /*width: 304px;*/
    z-index: 1;
}

修改
删除了不必要的步骤,更新了链接并添加了解释;)

我希望这有帮助!