CSS下拉菜单:z-index问题

时间:2015-07-01 21:56:26

标签: css css3 drop-down-menu css-transitions z-index

我的CSS下拉菜单出现了两个问题:JSFiddle

1)转换:当鼠标切换到打开它时,希望菜单向下滑动。之前打开的菜单应向上滑动以关闭。

#nav ul li ul
{
    -webkit-transition:height .5s ease-in;
    -moz-transition:height .5s ease-in;
    -o-transition:height .5s ease-in;
    -ms-transition:height .5s ease-in;
    transition:height .5s ease-in;
}

这似乎不起作用;可能是错误的" ul li"选择器,虽然我尝试了其他人。

2)将父项(" Menu1"," Menu2")置于菜单项列表的顶部,使其覆盖第一个孩子的顶部边框。也就是说," Menu1"之间应该没有边界。和" Item1"自" Menu1"没有底部边框,但看起来像是" Item1"是最重要的。

尝试使用z-index,但无法正常使用。没有过渡。

我意识到菜单有点复杂,可能有更多的CSS。

1 个答案:

答案 0 :(得分:0)

怎么样? JSFiddle

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

#nav ul li {
    display: inline-block;
    border: 1px solid #FFF;
    border-bottom: 0;
    border-radius: 5px;
    margin: 0 15px;
    position: relative;
    -webkit-transition: border-color 0.2s ease-in;
    -moz-transition: border-color 0.2s ease-in;
    -o-transition: border-color 0.2s ease-in;
    -ms-transition: border-color 0.2s ease-in;
    transition: border-color 0.2s ease-in;
}

#nav ul li a {
    color: #8799B3;
    display: block;
    padding: 10px 6px;
    border: 0;
    -webkit-transition: color 0.2s ease-in;
    -moz-transition: color 0.2s ease-in;
    -o-transition: color 0.2s ease-in;
    -ms-transition: color 0.2s ease-in;
    transition: color 0.2s ease-in;
}

#nav ul li:hover {
    color: #415161;
    border-color: #E9E9E9;
}

#nav ul li a:hover {
    color: #415161;
}

#nav ul li ul {
    opacity: 0;
    visibility: hidden;
    text-decoration: none;
    line-height: 1;
    color: #8799B3;
    background: #FFF;
    position: absolute;
    top: calc(100% - 5px);
    left: -1px;
    z-index: 1000;
    width: 165px;
    border: 0;
}

#nav ul li:hover ul {
    opacity: 1;
    visibility: visible;
}

#nav ul li ul li {
    border: 0;
    display: block;
    font-size: 14px;
    margin: 0;
    -webkit-transition: background 0.2s ease-in;
    -moz-transition: background 0.2s ease-in;
    -o-transition: background 0.2s ease-in;
    -ms-transition: background 0.2s ease-in;
    transition: background 0.2s ease-in;
}

#nav ul li ul li:hover {
    border: none;
    color: #415161;
    background: #F7F9FB;
}

#nav ul li ul li a {
    padding: 10px;
    border: none;
    color: #8799B3;
    border-left: 1px solid #E9E9E9;
    border-right: 1px solid #E9E9E9;
    border-bottom: 1px solid #E9E9E9;
}

#nav ul li ul li:first-child a {
    border-top: 1px solid #E9E9E9;
    border-radius: 0 5px 0 0;
}

#nav ul li ul li:last-child a {
    border-bottom: 1px solid #E9E9E9;
    border-radius: 0 0 5px 5px;
}

#nav ul li ul {
    -webkit-transition: opacity 0.2s ease-in;
    -moz-transition: opacity 0.2s ease-in;
    -o-transition: opacity 0.2s ease-in;
    -ms-transition: opacity 0.2s ease-in;
    transition: opacity 0.2s ease-in;
}
<div id="nav">
    <ul>
        <li>
            <a href="#">Menu1</a>
            <ul>
                <li><a href="menu.html">Item1</a></li>
                <li><a href="menu.html">Item2</a></li>
                <li><a href="menu.html">Item3</a></li>
                <li><a href="menu.html">Item4</a></li>
            </ul>
        </li>
        <li>
            <a href="#">Menu2</a>
            <ul>
                <li><a href="menu.html">Item1</a></li>
                <li><a href="menu.html">Item2</a></li>
                <li><a href="menu.html">Item3</a></li>
                <li><a href="menu.html">Item4</a></li>
            </ul>
        </li>
        <li>
            <a href="#">Menu3</a>
            <ul>
                <li><a href="menu.html">Item1</a></li>
                <li><a href="menu.html">Item2</a></li>
                <li><a href="menu.html">Item3</a></li>
                <li><a href="menu.html">Item4</a></li>
            </ul>
        </li>
    </ul>
</div>