是否可以使用 Jasny Bootstrap 将 Off-canvas 菜单与模态功能结合起来?

Is it possible to combine an Off-canvas menu with modal function using Jasny Bootstrap?

我在使用 Jasny Bootstrap

将 off-canvas 与模态功能结合时遇到问题

这些是我采取的步骤:

1. 我使用 jasny bootstrap 创建了一个非 canvas 菜单,它按预期工作 Off Canvas Only

2. 但是,当我添加模态功能时,将屏幕拉伸到 992px 宽度后,off-canvas 菜单会消失。此外,我无法通过在元素外部单击来关闭模式。 Modal functionality + Off-Canvas

我遇到的问题是 data-toggle="modal"data-toggle="offcanvas" 我只能用其中之一。还有另一种添加两个数据切换的方法吗? 或者更好:

What options do I have to use both an offcanvas menu with modal functionality?

先决条件:

HTML

   <div id="myModal" class="modal fade navmenu navmenu-default navmenu-fixed-left offcanvas-sm">
  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
  <a class="navmenu-brand visible-md visible-lg visible-sm visible-xs" href="#">Chronicles</a>
  <ul class="nav navmenu-nav">
    <li class="active"><a class="nav-link" href="index.html">Slide in</a></li>
    <li><a class="nav-link" href="http://www.jasny.net/bootstrap/examples/navmenu-push/">Push</a></li>
    <li><a class="nav-link" href="http://www.jasny.net/bootstrap/examples/navmenu-reveal/">Reveal</a></li>
    <li><a class="nav-link" href="http://www.jasny.net/bootstrap/examples/navbar-offcanvas/">Off canvas navbar</a></li>
  </ul>

</div>

<div class="navbar navbar-default navbar-fixed-top hidden-md hidden-lg navbar-preheader">
  <button type="button" class="navbar-toggle" data-toggle="modal" data-target=".navmenu, #myModal">
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
  </button>
  <a class="navbar-brand" href="#">Project name</a>
</div>

<div class="container">
  <div class="page-header">
    <h1>Navmenu Template</h1>
  </div>
  <p class="lead">This example shows the navmenu element. If the viewport is <b>less than 992px</b> the menu will be placed the off canvas and will be shown with a slide in effect.</p>

</div><!-- /.container -->

CSS:

    html, body {
  height: 100%;
}
body {
  padding: 50px 0 0 0;
}

.navmenu-fixed-left {
  z-index:1050;
  top: 0;
  bottom: 0; background:#fff!important;
}

.navbar-fixed-top {

  background:#fff!important;
}

.navbar {
  display: block;
  text-align: center;
}
.navbar-brand {
  display: inline-block;
  float: none; 
}
.navbar-toggle {
  position: absolute;
  float: left; 
  margin-left: 15px;
}

.container {
  max-width: 100%;
}

@media (min-width: 1px) {
  .navbar-toggle {
    display: block !important; background:none!important;  border:none !important; color:#f90 !important;
  }
}

@media (min-width: 992px) {
  body {
    padding: 0 0 0 300px;
  }
  .navmenu {
    padding-top: 0; 
  }
  .navbar {
    display: none !important; /* IE8 fix */
  }
}

    .navbar-default .navbar-toggle .icon-bar{
       background-color:#f90;
    }


.modal-body {
    max-height: calc(100vh - 210px);
    overflow-y: auto;
}
::-webkit-scrollbar {
    width: 12px;
}

::-webkit-scrollbar-track {
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3); 
    border-radius: 10px;
}

::-webkit-scrollbar-thumb {
    border-radius: 10px;
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.5); 
}

不,modal 和 offcanvas 不应在同一个元素上使用。意想不到的事情会发生。

如果您只想使用 offcanvas 添加背景,最好手动添加,而不是使用模态插件。

在HTML中添加

<div class="backdrop">

与CSS

.backdrop {
  background: rgba(0, 0, 0, 0.5);
  position: fixed;
  top: 0;
  bottom: 0;
  width: 100vw;
  height: 100vh;
  z-index: 1040;
  display: none;
}

与使用 JavaScript 相比,显示和隐藏背景

$('.navmenu').on('show.bs.offcanvas', function() {
    $('.backdrop').fadeIn();
});

$('.navmenu').on('hide.bs.offcanvas', function() {
    $('.backdrop').fadeOut();
});

最后,向菜单添加关闭按钮是微不足道的

<a href="#" class="close" data-toggle="offcanvas" data-target=".navmenu">&times;</a>

See the fiddle