z-index 不适用于 CSS 下拉菜单

发布于 2024-11-30 17:48:28 字数 2957 浏览 1 评论 0原文

我有一个 CSS 驱动的简单下拉菜单,可以在较窄的屏幕上以双行形式显示。

当我激活顶行项目之一的下拉菜单时,下拉菜单将显示在与底行相同的“Z”级别。

我以为我已经按要求设置了 z 索引,但它在 Chrome、Firefox、Safari 或 iPhone 中不起作用。

这是一个示例: http://jsfiddle.net/bNBp3/ - “Meat”附加了一个下拉菜单。

<ul id="main-navigation">
    <li><a href="/" title="Go to the Home page"><span>Home</span></a>
    </li>
    <li><a href="/meat/" title="Go to the Meat page"><span>Meat</span></a>
        <ul>
            <li><a href="/meat/bacon/" title="Go to the Bacon page"><span>Bacon</span></a></li>
            <li><a href="/meat/ham/" title="Go to the Ham page"><span>Ham</span></a></li>
            <li><a href="/meat/pork/" title="Go to the Pork page"><span>Pork</span></a></li>
        </ul>
    </li>
    <li><a href="/vegetables/" title="Go to the Vegetables page"><span>Vegetables</span></a>
    </li>
    <li><a href="/about-us/" title="Go to the About Us page"><span>About Us</span></a>
    </li>
    <li><a href="/contact-us/" title="Go to the Contact Us page"><span>Contact Us</span></a>
    </li>
    <li><a href="/a-z/" title="Go to the A-Z page"><span>A-Z</span></a>
    </li>
</ul> 
ul#main-navigation {
    display:block;
    border-bottom:1px solid #ccc;
    margin:0 auto 0px auto;
    min-height:21px;
    padding:0;
    width:100%;
    z-index:11;
    clear:both;
}
ul#main-navigation li {
    display:block;
    float:left;
    list-style:none;
    margin:0 5px 0 0;
    padding:0;
    position:relative;
    z-index:12;
    min-height:22px;
    min-width:30%;
}
ul#main-navigation li a {
    color:#000;
    display:block;
    padding:2px;
    text-decoration:none;
    z-index:13;
}
ul#main-navigation li a.current, ul#main-navigation li a.section {
    color:#fff;
}
ul#main-navigation li ul {
    background:#eee;
    position:absolute;
    left:-1px;
    top:21px;
    display:none;
    overflow:hidden;
    padding:0px;
    z-index:14;
    width:100%;
    border:1px solid #ccc;
}
ul#main-navigation li ul li {
    display:block;
    float:none;
    margin:0;
    z-index:15;
    width:100%;
    border-bottom:1px solid #ccc;
}
ul#main-navigation li ul li a {
    background:#eee;
    display:block;
    color:#333;
    z-index:16;
    zoom:100%;     
}
ul#main-navigation li ul li a:hover {
    background:#fff;
}
ul#main-navigation li:hover {
    background:#eee;
    border-left:1px solid #ccc;
    border-right:1px solid #ccc;
}
ul#main-navigation li:hover a.current {
    background:#eee; color:#000;
}
ul#main-navigation li:hover ul {
    display:block;
}

I have a CSS driven simple drop down menu that can appear in double rows on narrower screens.

When I activate the dropdown of one of the top row items the drop down appears at the same "Z" level as the bottom row.

I thought I've set the z-indexes as required but it isn't working in Chrome, Firefox, Safari or iPhone.

Here is a sample: http://jsfiddle.net/bNBp3/ - "Meat" has a dropdown attached.

<ul id="main-navigation">
    <li><a href="/" title="Go to the Home page"><span>Home</span></a>
    </li>
    <li><a href="/meat/" title="Go to the Meat page"><span>Meat</span></a>
        <ul>
            <li><a href="/meat/bacon/" title="Go to the Bacon page"><span>Bacon</span></a></li>
            <li><a href="/meat/ham/" title="Go to the Ham page"><span>Ham</span></a></li>
            <li><a href="/meat/pork/" title="Go to the Pork page"><span>Pork</span></a></li>
        </ul>
    </li>
    <li><a href="/vegetables/" title="Go to the Vegetables page"><span>Vegetables</span></a>
    </li>
    <li><a href="/about-us/" title="Go to the About Us page"><span>About Us</span></a>
    </li>
    <li><a href="/contact-us/" title="Go to the Contact Us page"><span>Contact Us</span></a>
    </li>
    <li><a href="/a-z/" title="Go to the A-Z page"><span>A-Z</span></a>
    </li>
</ul> 
ul#main-navigation {
    display:block;
    border-bottom:1px solid #ccc;
    margin:0 auto 0px auto;
    min-height:21px;
    padding:0;
    width:100%;
    z-index:11;
    clear:both;
}
ul#main-navigation li {
    display:block;
    float:left;
    list-style:none;
    margin:0 5px 0 0;
    padding:0;
    position:relative;
    z-index:12;
    min-height:22px;
    min-width:30%;
}
ul#main-navigation li a {
    color:#000;
    display:block;
    padding:2px;
    text-decoration:none;
    z-index:13;
}
ul#main-navigation li a.current, ul#main-navigation li a.section {
    color:#fff;
}
ul#main-navigation li ul {
    background:#eee;
    position:absolute;
    left:-1px;
    top:21px;
    display:none;
    overflow:hidden;
    padding:0px;
    z-index:14;
    width:100%;
    border:1px solid #ccc;
}
ul#main-navigation li ul li {
    display:block;
    float:none;
    margin:0;
    z-index:15;
    width:100%;
    border-bottom:1px solid #ccc;
}
ul#main-navigation li ul li a {
    background:#eee;
    display:block;
    color:#333;
    z-index:16;
    zoom:100%;     
}
ul#main-navigation li ul li a:hover {
    background:#fff;
}
ul#main-navigation li:hover {
    background:#eee;
    border-left:1px solid #ccc;
    border-right:1px solid #ccc;
}
ul#main-navigation li:hover a.current {
    background:#eee; color:#000;
}
ul#main-navigation li:hover ul {
    display:block;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝天白云 2024-12-07 17:48:28

只需删除所有 z-index 值即可正常工作:

http:// jsfiddle.net/bNBp3/12/

在 Chrome 和 Firefox6 中测试。

编辑:我似乎忘记从演示中的 ul#main-navigation li ul 中删除 z-index,这就是它起作用的原因,所以只需将其设置为任何值 1 或更高即可。

Just get rid of all the z-index values and it works fine:

http://jsfiddle.net/bNBp3/12/

Tested in Chrome and Firefox6.

Edit: It seems I forgot to remove the z-index from ul#main-navigation li ul in the demo and that's what makes it work, so just set it to any value 1 or higher.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文