gpt4 book ai didi

javascript - 单击第二个下拉黑框时应保持可见

转载 作者:技术小花猫 更新时间:2023-10-29 12:25:54 24 4
gpt4 key购买 nike

在我正在构建的这个下拉导航中,如果打开了一个下拉菜单并且您单击以打开第二个下拉菜单,黑框应该保持可见。目前,当您点击第二个下拉菜单时黑框消失,并在下拉菜单完全打开后重新出现。

更新

我还注意到打开下拉菜单后会显示黑框,它应该同时打开。

我希望这是有道理的,感谢您的帮助!

$(document).ready(function() {
$(".click").on("click", function(e) {
var menu = $(this);
toggleDropDown(menu);
});

$(document).on('mouseup',function(e) {
var container = $("nav .top-bar-section ul");


// if the target of the click isn't the container nor a descendant of the container
if (!container.is(e.target) && container.has(e.target).length === 0) {
$('a.active').parent().find('.showup').stop(true, true).slideUp(500, function() {
$(".main-container").removeClass("black-bg");
if ($('a.active').hasClass('active')) {
$('a.active').removeClass('active');
}
});
}
});

});


function toggleDropDown(menu) {
var isActive = $('a.active').length;
$('a.active').parent().find('.showup').stop(true, true).slideUp(500, function() {
$(".main-container").removeClass("black-bg");
if (menu.hasClass('active')) {
menu.removeClass('active');
} else {
$('a.active').removeClass('active');
menu.addClass('active');
menu.parent().find('.showup').stop(true, true).slideDown(500, function() {
$(".main-container").addClass("black-bg");
});
}
});
if (!isActive) {
menu.addClass('active');
menu.parent().find('.showup').stop(true, true).slideDown(500, function() {
$(".main-container").addClass("black-bg");
});
}

}
* {
box-sizing: border-box;
}

body {
margin: 0;
}

.nav-wrapper {
width: 100%;
overflow: hidden;
background: #424242;
}

nav {
width: 1024px;
margin: auto;
overflow: hidden;
background: #424242;
}

.nav-content {
width: 100%;
z-index: 999;
background: #ccc;
}

.top-bar-section {
float: left;
}
.top-bar-section a.active {
background: #f00;
}


.showup {
display: none;
background: #ccc;
position: absolute;
width: 100%;
top: 70px;
left: 0;
z-index: 99;
padding: 30px 15px 30px 20px;
}

p {
font-size: 14px;
line-height: 1.4;
}

li.nav-item {
display: inline-block;
background: #f5f5f5;
}
li.nav-item a {
display: block;
text-decoration: none;
padding: 10px;
}
.main-container {
width: 80%;
height: 400px;
margin: auto;
}
.black-bg {
background: #000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="nav-wrapper">
<nav>
<div class="top-bar-section">
<ul>
<li class="nav-item">
<a href="#" class="click">Nav item 1</a>
<div class="showup">
<div class="nav-content">
<p>
Dropdown for Nav Item 1.
</p>
</div>
</div>
</li>
<li class="nav-item">
<a href="#" class="click">Nav item 2</a>
<div class="showup">
<div class="nav-content">
<p>
Dropdown for Nav Item 2.
</p>
</div>
</div>
</li>
<li class="nav-item">
<a href="#" class="click">Nav item 3</a>
<div class="showup">
<div class="nav-content">
<p>
Dropdown for Nav Item 3.
</p>
</div>
</div>
</li>
<li class="nav-item">
<a href="#" class="click">Nav item 4</a>
<div class="showup">
<div class="nav-content">
<p>
Dropdown for Nav Item 4.
</p>
</div>
</div>
</li>
</ul>
</div>
</nav>
</div>
<div class="main-container">

</div>

最佳答案

如果您希望在单击菜单后添加 black-bg,则不要在每次单击事件中删除和添加 black-bg 类。如果菜单有 active 类,只需添加一次,如果菜单没有 active 类,则将其删除。如果您在每次点击事件中删除和添加类,那么 black-bg 将先消失然后再次出现。在打开下拉菜单时 black-bg 然后从回调函数中删除 $(".main-container").addClass("black-bg"); slideDown() 因为当前效果完成后执行回调函数。

$(document).ready(function() {
$(".click").on("click", function(e) {
var menu = $(this);
toggleDropDown(menu);
});

$(document).on('mouseup',function(e) {
var container = $("nav .top-bar-section ul");


// if the target of the click isn't the container nor a descendant of the container
if (!container.is(e.target) && container.has(e.target).length === 0) {
$('a.active').parent().find('.showup').stop(true, true).slideUp(500, function() {
$(".main-container").removeClass("black-bg");
if ($('a.active').hasClass('active')) {
$('a.active').removeClass('active');
}
});
}
});

});


function toggleDropDown(menu) {
var isActive = $('a.active').length;
$('a.active').parent().find('.showup').stop(true, true).slideUp(500, function() {
if (menu.hasClass('active')) {
menu.removeClass('active');
$(".main-container").removeClass("black-bg");
} else {
$('a.active').removeClass('active');
menu.addClass('active');
menu.parent().find('.showup').stop(true, true).slideDown(500, function() {
});
}
});
if (!isActive) {
menu.addClass('active');
menu.parent().find('.showup').stop(true, true).slideDown(500);
$(".main-container").addClass("black-bg");
}

}
* {
box-sizing: border-box;
}

body {
margin: 0;
}

.nav-wrapper {
width: 100%;
overflow: hidden;
background: #424242;
}

nav {
width: 1024px;
margin: auto;
overflow: hidden;
background: #424242;
}

.nav-content {
width: 100%;
z-index: 999;
background: #ccc;
}

.top-bar-section {
float: left;
}
.top-bar-section a.active {
background: #f00;
}


.showup {
display: none;
background: #ccc;
position: absolute;
width: 100%;
top: 70px;
left: 0;
z-index: 99;
padding: 30px 15px 30px 20px;
}

p {
font-size: 14px;
line-height: 1.4;
}

li.nav-item {
display: inline-block;
background: #f5f5f5;
}
li.nav-item a {
display: block;
text-decoration: none;
padding: 10px;
}
.main-container {
width: 80%;
height: 400px;
margin: auto;
}
.black-bg {
background: #000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="nav-wrapper">
<nav>
<div class="top-bar-section">
<ul>
<li class="nav-item">
<a href="#" class="click">Nav item 1</a>
<div class="showup">
<div class="nav-content">
<p>
Dropdown for Nav Item 1.
</p>
</div>
</div>
</li>
<li class="nav-item">
<a href="#" class="click">Nav item 2</a>
<div class="showup">
<div class="nav-content">
<p>
Dropdown for Nav Item 2.
</p>
</div>
</div>
</li>
<li class="nav-item">
<a href="#" class="click">Nav item 3</a>
<div class="showup">
<div class="nav-content">
<p>
Dropdown for Nav Item 3.
</p>
</div>
</div>
</li>
<li class="nav-item">
<a href="#" class="click">Nav item 4</a>
<div class="showup">
<div class="nav-content">
<p>
Dropdown for Nav Item 4.
</p>
</div>
</div>
</li>
</ul>
</div>
</nav>
</div>
<div class="main-container">

</div>

关于javascript - 单击第二个下拉黑框时应保持可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50524104/

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