我正在使用Bootstrap 4文档中的offcanvas实验代码,但是当您在菜单外单击时,它不是设计为关闭的,这是我真的希望这样做。
我已经尝试了Web上的多个jquery代码段,但是它们利用了bootstrap的.collapse,并且在编辑它们以包含自定义.offcanvas-collapse类时不起作用。
<nav class="navbar fixed-top navbar-dark bg-dark">
<button class="navbar-toggler p-0 border-0" type="button" data-toggle="offcanvas" data-target="#offcanvasNav" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
<i data-feather="menu"></i>
</button>
<a class="navbar-brand" href="#">Sleight of Word</a>
<button class="navbar-toggler p-0 border-0" type="button" data-toggle="searchBar">
<i data-feather="search"></i>
</button>
<form class="form-inline my-2 my-lg-0 d-none">
<input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
<div class="navbar-collapse offcanvas-collapse" id="offcanvasNav">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Dashboard <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Notifications</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Profile</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Switch account</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="dropdown01" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Settings</a>
<div class="dropdown-menu" aria-labelledby="dropdown01">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
.offcanvas-collapse {
position: fixed;
top: 56px; /* Height of navbar */
bottom: 0;
right: 100%;
width: 240px;
padding-right: 1rem;
padding-left: 1rem;
overflow-y: auto;
visibility: hidden;
background-color: purple;
transition: visibility .2s ease-in-out, -webkit-transform .2s ease-in-out;
transition: transform .2s ease-in-out, visibility .2s ease-in-out;
transition: transform .2s ease-in-out, visibility .2s ease-in-out, -webkit-transform .2s ease-in-out;
}
.offcanvas-collapse.open {
visibility: visible;
-webkit-transform: translateX(100%);
transform: translateX(100%);
}
$(function () {
'use strict'
$('[data-toggle="offcanvas"]').on('click', function () {
$('.offcanvas-collapse').toggleClass('open')
})
})
$(document).on('click',function(){
$('.offcanvas-collapse').offcanvas-collapse('hide');
})
我希望这将使Offcanvas导航可以通过菜单按钮切换(打开和关闭),并且在菜单外部单击时可以关闭导航,但是在导航外部单击则无济于事。
最佳答案
$(document).on('click',function(){
$('.offcanvas-collapse').toggleClass('open');
})
单独,将在文档中所有具有类
open
的元素上切换类offcanvas-collapse
。如果元素具有类,则将其删除,否则将添加它。如果将以上内容与其他代码段结合使用:
$(function () {
'use strict'
$('[data-toggle="offcanvas"]').on('click', function () {
$('.offcanvas-collapse').toggleClass('open')
})
})
...,每次单击
[data-toggle="offcanvas"]
时,两个代码段都在运行(因为您单击了[data-toggle="offcanvas"]
,但同时也单击了document
(因为click
事件一直冒泡直至document
,除非因此,offcanvas-collapse
类被切换了两次,因此看起来好像不起作用。这显然不是您要追求的。
您可能想要:
单击
open
时,在.offcanvas-collapse
元素上切换类[data-toggle="offcanvas"]
,而不允许该click
事件传播到document
。当在文档中的任何位置单击时,从
open
元素中删除(而不切换)类.offcanvas-collapse
。如果使用toggle
,则关闭offcanvas时,click
中的document
将打开它,这可能不是您想要的。话虽如此,这是实际的做法:
$(function () {
'use strict'
$(document)
.on('click', '[data-toggle="offcanvas"]', function(e) {
$('.offcanvas-collapse').toggleClass('open'); // toggle `.open`
e.stopPropagation(); // and stop propagation
})
.on('click', function() {
$('.offcanvas-collapse').removeClass('open'); // remove `.open`
});
})
编辑:根据评论请求,仅在单击导航栏外部时关闭画布,将第二个
.on()
替换为以下内容:.on('click', function(e) {
if (!$(e.target).closest('nav.fixed-top').is('nav')) {
$('.offcanvas-collapse').removeClass('open'); // remove `.open`
}
});
它检查目标是否位于类
<nav>
的fixed-top
中。关于jquery - 我如何在外部单击以关闭 bootstrap 文档中的此自定义offcanvas导航?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57259093/