gpt4 book ai didi

javascript - stopPropagation 防止引导下拉

转载 作者:行者123 更新时间:2023-11-29 20:48:23 27 4
gpt4 key购买 nike

我使用 jquery 来隐藏和显示侧边栏,如果我在外部单击或调整侧边栏隐藏的大小,但我遇到了一个问题,所以我的所有下拉列表都停止了,因为我使用了 stopPropagation

我的 HTML 是

$('#btn-menusb-l').click(function(e){
e.stopPropagation();
$('#sdbr-l').toggleClass("ly-bmk-sidebar-l-mob");
});
$('#sdbr-l').click(function(e){
e.stopPropagation();
});
$('body,html').click(function(e){
$('#sdbr-l').removeClass('ly-bmk-sidebar-l-mob');
});
$(window).resize(function(){
$('#sdbr-l').removeClass('ly-bmk-sidebar-l-mob');
});
.ly-bmk-header
{
border:0;
border-bottom:1px solid #bbb;
margin:0 auto;
height: 50px;
position: relative;
min-width: 360px;
width: 100%;
z-index: auto !important;
padding: 0 10px
}

.hdr-table
{
display: table;
width: 100%;
}
.hdr-cell
{
display: table-cell;
vertical-align: middle;
}




.ly-bmk-layout
{
display: table-row;
}

.ly-bmk-cell
{
display: table-cell;
vertical-align: top;
}


.ly-bmk-sidebar-l, .ly-bmk-sidebar-l-mob
{
background: #FFFFFF;
border-right:1px solid #A6A6A6;
margin:0;
width: 330px;
}

.ly-bmk-content
{
margin:0 auto;
}

@media all and (max-width: 750px)
{
.ly-bmk-sidebar-l
{position:fixed;margin-left:-340px}
.ly-bmk-sidebar-l.ly-bmk-sidebar-l-mob
{margin-left:0px;position:fixed;z-index:11111;overflow-y:auto;
-webkit-transform: translate3d(0px, 0, 0);
-moz-transform: translate3d(0, 0, 0);
-o-transform: translate3d(0, 0, 0);
-ms-transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0);
-webkit-transition: all 0.3s linear;
-moz-transition: all 0.3s linear;
-ms-transition: all 0.3s linear;
-o-transition: all 0.3s linear;
transition: all 0.3s linear;
height: 100%;
top: 0;
}
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<body>
<div class="ly-bmk-sheet">
<header class="ly-bmk-header">
<div class="hdr-table">
<div class="hdr-cell">
<nav class="ly-bmk-navc">
<ul class="ly-bmk-hmenu">
<li><a href="#">Logo</a></li>
</ul>
</nav>
</div>
<div class="hdr-cell">
<nav class="ly-bmk-navc">
<ul class="ly-bmk-hmenu">
<li><a id="btn-menusb-l" href="#">Sidebar</a></li>
</ul>
</nav>
</div>
<div class="hdr-cell">
<nav class="ly-bmk-navc">
<ul class="ly-bmk-hmenu">
<li class="dropdown">
<a data-toggle="dropdown">DropDwon Header</a>
<ul class="dropdown-menu">
<li><a href="#">HTML</a></li>
<li><a href="#">CSS</a></li>
<li><a href="#">JavaScript</a></li>
</ul>
</li>
</ul>
</nav>
</div>
</div>
</header>
<div class="ly-bmk-layout">
<div id="sdbr-l" class="ly-bmk-cell ly-bmk-sidebar-l">

<div class="ly-bmk-vmenu-content">
<ul class="ly-bmk-vmenu">
<li class="dropdown">
<a data-toggle="dropdown">DropDwon SideBar</a>
<ul class="dropdown-menu">
<li><a href="#">HTML</a></li>
<li><a href="#">CSS</a></li>
<li><a href="#">JavaScript</a></li>
</ul>
</li>
<li><a href="#">Test</a></li>
</ul>
</div>
</div>
</div>
<div class="ly-bmk-cell ly-bmk-content">
<p>WELCOME</p>
</div>
</div>
</body>

如果我点击里面我想保持边栏显示但是如果我点击外面隐藏,我的jquery代码工作除了它阻止我的下拉菜单显示

  • 如果宽度小于 750px 则隐藏侧边栏
  • 页眉中的侧边栏,如果宽度小于 750 像素则显示侧边栏
  • 侧边栏内的下拉菜单不起作用,但标题栏中的下拉菜单起作用

最佳答案

注意:我没有仔细研究这个。可能存在更好的答案,在这种情况下,我可能会删除它。由于还没有其他答案,我提供此答案的基础是部分/不完整的答案可能比没有答案好,并将其留给社区更新/编辑。


区分元素

在您的点击事件中,您需要确定何时停止传播。下面是通过嗅探目标元素并评估其祖先来完成的。

我获取父元素的类和最接近的 UL 的类(如果它存在)并检查该类是否是 dropdown(按钮)或 下拉菜单(菜单列表)。如果是,则事件可以继续,否则传播停止:

jQuery(document).ready(function($) {

$('#btn-menusb-l').click(function(e) {
e.stopPropagation();
$('#sdbr-l').toggleClass("ly-bmk-sidebar-l-mob");
});

$('#sdbr-l').click(function(e) {
let classes = Array.from(e.target.classList)
if (e.target.closest('ul') != null)
classes=classes.concat(Array.from(e.target.closest('ul').classList))
if (e.target.parentElement != null)
classes=classes.concat(Array.from(e.target.parentElement.classList))

if (!(classes.includes('dropdown') || classes.includes('dropdown-menu')))
e.stopPropagation()
});

$('body,html').click(function(e) {
let classes = Array.from(e.target.classList)
if (e.target.closest('ul') != null)
classes=classes.concat(Array.from(e.target.closest('ul').classList))
if (e.target.parentElement != null)
classes=classes.concat(Array.from(e.target.parentElement.classList))

if (!(classes.includes('dropdown') || classes.includes('dropdown-menu')))
$('#sdbr-l').removeClass('ly-bmk-sidebar-l-mob');
});

$(window).resize(function() {
$('#sdbr-l').removeClass('ly-bmk-sidebar-l-mob');
});

})
.ly-bmk-header {
border: 0;
border-bottom: 1px solid #bbb;
margin: 0 auto;
height: 50px;
position: relative;
min-width: 360px;
width: 100%;
z-index: auto !important;
padding: 0 10px
}

.hdr-table {
display: table;
width: 100%;
}

.hdr-cell {
display: table-cell;
vertical-align: middle;
}

.ly-bmk-layout {
display: table-row;
}

.ly-bmk-cell {
display: table-cell;
vertical-align: top;
}

.ly-bmk-sidebar-l,
.ly-bmk-sidebar-l-mob {
background: #FFFFFF;
border-right: 1px solid #A6A6A6;
margin: 0;
width: 330px;
}

.ly-bmk-content {
margin: 0 auto;
}

@media all and (max-width: 750px) {
.ly-bmk-sidebar-l {
position: fixed;
margin-left: -340px
}
.ly-bmk-sidebar-l.ly-bmk-sidebar-l-mob {
margin-left: 0px;
position: fixed;
z-index: 11111;
overflow-y: auto;
-webkit-transform: translate3d(0px, 0, 0);
-moz-transform: translate3d(0, 0, 0);
-o-transform: translate3d(0, 0, 0);
-ms-transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0);
-webkit-transition: all 0.3s linear;
-moz-transition: all 0.3s linear;
-ms-transition: all 0.3s linear;
-o-transition: all 0.3s linear;
transition: all 0.3s linear;
height: 100%;
top: 0;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />


<div class="container">
<div class="ly-bmk-sheet">
<header class="ly-bmk-header">
<div class="hdr-table">
<div class="hdr-cell">
<nav class="ly-bmk-navc">
<ul class="ly-bmk-hmenu">
<li><a href="#">Logo</a></li>
</ul>
</nav>
</div>
<div class="hdr-cell">
<nav class="ly-bmk-navc">
<ul class="ly-bmk-hmenu">
<li><a id="btn-menusb-l" href="#">Sidebar</a></li>
</ul>
</nav>
</div>
<div class="hdr-cell">
<nav class="ly-bmk-navc">
<ul class="ly-bmk-hmenu">
<li class="dropdown">
<a data-toggle="dropdown">DropDown Header</a>
<ul class="dropdown-menu">
<li><a href="#">HTML</a></li>
<li><a href="#">CSS</a></li>
<li><a href="#">JavaScript</a></li>
</ul>
</li>
</ul>
</nav>
</div>
</div>
</header>
<div class="ly-bmk-layout">
<div id="sdbr-l" class="ly-bmk-cell ly-bmk-sidebar-l">

<div class="ly-bmk-vmenu-content">
<ul class="ly-bmk-vmenu">
<li class="dropdown">
<a data-toggle="dropdown">DropDown SideBar</a>
<ul class="dropdown-menu">
<li><a href="#">HTML</a></li>
<li><a href="#">CSS</a></li>
<li><a href="#">JavaScript</a></li>
</ul>
</li>
<li><a href="#">Test</a></li>
</ul>
</div>
</div>
</div>
<div class="ly-bmk-cell ly-bmk-content">
<p>WELCOME</p>
</div>
</div>
</div>

关于javascript - stopPropagation 防止引导下拉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53253336/

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