gpt4 book ai didi

jquery - 重叠的导航下拉菜单无法使 z-index 正确

转载 作者:行者123 更新时间:2023-11-28 04:08:58 25 4
gpt4 key购买 nike

我正在使用 Bootstrap,但我遇到了下拉菜单问题。我有一些 JS 会改变下拉菜单的位置,如果它要碰到窗口的边缘的话。它将它移动到父下拉列表的另一侧。问题是下拉菜单与根下拉菜单重叠并且 z-index 关闭。

我已经尝试调整所有我能想到的 z-index 但没有成功。有什么想法吗?

HTML:

<header>
<div class="container-fluid container-nav">
<div class="container container-wo-pad">
<div class="header-home">
<div class="site-title">
<a href="/"><img class="logo" src="">
<h1>Site</h1></a>
</div>
</div>
<!-- navigation object : U Navigation Items -->
<div class="nav">
<div class="navControl">
<a class="nav-btn" href="#"><i class="fa fa-bars" aria-hidden="true"></i> Navigation</a>
</div>

<ul class="nav-root">
<li class="has-sub nav-root-item">
<a href="">LinkLinkLinkLink <span class="show-carrot">&#9662;</span> </a>
<span class="open-icon"><i class="fa fa-plus open-close" aria-hidden="true"></i></span>
<ul class="dropdown sub-drop">
<li><a href="">Link</a></li>
<li class="has-sub">
<a href="">Link <span class="show-carrot">&#9662;</span> </a>
<span class="open-icon"><i class="fa fa-plus open-close" aria-hidden="true"></i></span>
<ul class="dropdown sub-drop">
<li><a href="">Link</a></li>
<li><a href="">Link</a></li>
<li><a href="">Link</a></li>
</ul>
</li>
<li class="has-sub">
<a href="">Link <span class="show-carrot">&#9662;</span> </a>
<span class="open-icon"><i class="fa fa-plus open-close" aria-hidden="true"></i></span>
<ul class="dropdown sub-drop">
<li><a href="">Link</a></li>
<li><a href="">Link</a></li>
<li><a href="">Link</a></li>
<li><a href="">Link</a></li>
</ul>
</li>
<li><a href="">Link</a></li>
<li><a href="">Link</a></li>
<li class="has-sub">
<a href="">Link <span class="show-carrot">&#9662;</span> </a>
<span class="open-icon"><i class="fa fa-plus open-close" aria-hidden="true"></i></span>
<ul class="dropdown sub-drop">
<li><a href="">Link</a></li>
<li><a href="">Link</a></li>
<li><a href="">Link</a></li>
</ul>
</li>
<li class="has-sub">
<a href="">Link <span class="show-carrot">&#9662;</span> </a>
<span class="open-icon"><i class="fa fa-plus open-close" aria-hidden="true"></i></span>
<ul class="dropdown sub-drop">
<li><a href="">Link</a></li>
<li><a href="">Link</a></li>
</ul>
</li>
<li class="has-sub">
<a href="">Link <span class="show-carrot">&#9662;</span> </a>
<span class="open-icon"><i class="fa fa-plus open-close" aria-hidden="true"></i></span>
<ul class="dropdown sub-drop">
<li><a href="">Link</a></li>
<li><a href="">Link</a></li>
<li><a href="">Link</a></li>
<li><a href="">Link</a></li>
<li><a href="">Link</a></li>
<li><a href="">Link</a></li>
</ul>
</li>
<li><a href="">Link</a></li>
<li><a href="" target="_blank">Link</a></li>
</ul>
</li>
<li class="has-sub nav-root-item">
<a href="">This one <span class="show-carrot">&#9662;</span> </a>
<span class="open-icon"><i class="fa fa-plus open-close" aria-hidden="true"></i></span>
<ul class="dropdown sub-drop">
<li class="has-sub">
<a href="">Then this one <span class="show-carrot">&#9662;</span> </a>
<span class="open-icon"><i class="fa fa-plus open-close" aria-hidden="true"></i></span>
<ul class="dropdown sub-drop">
<li><a href="">Link</a></li>
<li class="has-sub">
<a href="">Link <span class="show-carrot">&#9662;</span> </a>
<span class="open-icon"><i class="fa fa-plus open-close" aria-hidden="true"></i></span>
<ul class="dropdown sub-drop">
<li><a href="">Link </a></li>
<li><a href="">Link</a></li>
<li><a href="" target="_blank">Link</a></li>
</ul>
</li>
<li class="has-sub">
<a href="">Last One <span class="show-carrot">&#9662;</span> </a>
<span class="open-icon"><i class="fa fa-plus open-close" aria-hidden="true"></i></span>
<ul class="dropdown sub-drop">
<li><a href="">LinkLinkLinkLinkLink</a></li>
<li><a href="">Link</a></li>
</ul>
</li>
<li><a href="">Link</a></li>
<li><a href="">Link</a></li>
<li><a href="">Link</a></li>
</ul>
</li>
<li><a href="">Link Link Link Link</a></li>
<li><a href="">Link</a></li>
<li><a href="">Link</a></li>
</ul>
</li>
<li class="nav-root-item"><a href="">Link</a></li>
<li class="nav-root-item"><a href="">Link</a></li>
<li class="has-sub nav-root-item">
<a href="">Link <span class="show-carrot">&#9662;</span> </a>
<span class="open-icon"><i class="fa fa-plus open-close" aria-hidden="true"></i></span>
<ul class="dropdown sub-drop">
<li><a href="">Sub link 1</a></li>
<li><a href="">Sub link 2</a></li>
<li><a href="">Sub link 3</a></li>
<li class="has-sub">
<a href="">dropdown <span class="show-carrot">&#9662;</span> </a>
<span class="open-icon"><i class="fa fa-plus open-close" aria-hidden="true"></i></span>
<ul class="dropdown sub-drop">
<li><a href="">Sub link 1</a></li>
<li><a href="">Sub link 2</a></li>
<li><a href="">Sub link 3</a></li>
<li><a href="">Sub link 4</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>
</div>
</header>

CSS:

.container-nav {
background-color: #00bcd4;
margin-top: -1px; /* compensate for when alert isn't there */
padding-bottom: 10px;
}

.nav .navControl {
display: none;
cursor: pointer;
font-size: 16px;
border: 1px solid rgba(0, 0, 0, 0.1);
margin: 10px 0;
}

.open-icon {
float: right;
padding: 15px 20px;
}

.nav ul {
list-style: none;
margin-top: 0;
margin-bottom: 0;
padding: 0;
border-top: 1px solid rgba(0, 0, 0, 0.1);
}

.nav li {
/* margin-left: -4px; */
display: block;
position: relative;
z-index: 1;
font-size: 16px;
border-bottom: 1px solid rgba(0, 0, 0, 0.1);
}

.nav a {
padding: 15px 20px;
display: inline-block;
color: #fff;
text-decoration: none;
text-align: left;
}

.nav a:hover {
color: #fff;
}

.nav-root li a {
text-decoration: underline;
}

.nav .dropdown {
text-align: left;
width: 100%;
}

.nav .dropdown li {
margin: 0;
display: block;
}

.nav .dropdown a {
padding: 15px 20px;
border-right: 0;
margin-right: 10px;
text-shadow: 0px 1px 1px #b9afaf;
}

.nav .dropdown li:last-child a {
margin-right: 0;
}

.has-sub .open-icon {
border-left: 1px solid rgba(0, 0, 0, 0.1);
}

.sub-drop {
display: none;
background-color: #0097A7;
}



/* Custom, iPhone Retina */
@media only screen and (min-width : 320px) {
.site-title {
width: 100%;
}
.site-title h1{
font-size: 33px;
}
.search {
width: 100%
}
}

/* Extra Small Devices, Phones */
@media only screen and (min-width : 480px) {

}
@media all and (max-width: 600px) {
.site-title {
width: 100%;
text-align: center;
}
.search {
width: 100%
}
.nav .navControl {
display: block;
}

.nav-root {
display: none;
}

.show-carrot {
display: none;
}
}

@media all and (min-width: 600px) {
.site-title {
display: table-cell;
width: 50%;
vertical-align: middle;
}
.search {
display: table-cell;
width: 50%;
vertical-align: middle;
}
.search-field {
width: 50%;
}
.logo {
width: 50px;
}
.home-container {
margin-top: -34px;
}

.container-no-pad {
width: 100%;
margin: 0 auto;
}
.container-nav {
height: 189px;
}

.nav {
position: relative;
z-index: 5;
max-width: 1170px;
margin: 0 auto;
}
.nav-root {
padding-left: 0;
display: block;
}

.nav-root li a {
text-decoration: none;
text-align: left;
}

.nav ul {
border: 0;
white-space: nowrap;
}

.nav li {
/* margin-right: -4px; */
display: inline-block;
border: 0;
}

.nav a {
width: 100%;
}

.nav a:hover {

}

.nav .dropdown {
display: none;
background: #80DEEA;
position: absolute;
top: 100%;
left: 0;
width: auto;
}

.nav .dropdown li {
display: block;
}

.nav .dropdown a {
margin-right: 0;
white-space: nowrap;
}
.nav .dropdown a:hover {
background: #00bcd4;
}

.nav span {
display: inline;
position: relative;
left: 5px;
}

.nav a:hover + .dropdown,
.nav .dropdown:hover {
display: block;
}

/* desktop hover sub dropdown show */
.nav a:hover + span + .dropdown {
display: block;
}

.dropdown .dropdown {
left: 100%;
top: 0;
}

.sub-drop {
display: block;
box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
}

.open-icon {
display: none !important;
}
.show-carrot {
display: block;
}
}

/*----------------------------------------------------------------------------------------------------------------*\
NAV dropdown z-index
\*----------------------------------------------------------------------------------------------------------------*/
.dropdown {
z-index: 1000;
}

.dropdown .dropdown {
z-index: 2000;
}

.dropdown .dropdown .dropdown {
z-index: 3000;
}

.dropdown li {
z-index: 1001;
}

.dropdown .dropdown li {
z-index: 2002;
}

.dropdown .dropdown .dropdown li {
z-index: 3003;
}

jQuery:

$(document).ready(function() {

$('.dropdown').parent().hover(function() {
var menu = $(this).find("ul");
var menupos = $(menu).offset();

if (menupos.left + menu.width() > $(window).width()) {
var newpos = -$(menu).width();
menu.css({ left: newpos });
}
});
});

Link to Fiddle

问题图片: Menu overlap

最佳答案

我觉得你那里有很多不必要的 css 样式,但主要是你缺少最后一个 ul 上的 z-index(我用类 newStyle 更新了它)。

https://jsfiddle.net/ctxp4f3u/4/

.newStyle {
position: absolute;
z-index: 3000;
}

关于jquery - 重叠的导航下拉菜单无法使 z-index 正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42816506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com