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

时间:2017-01-26 03:24:43

标签: css3 twitter-bootstrap-3 bootstrap-modal jasny-bootstrap off-canvas-menu

我在使用Jasny Bootstrap

将非画布与模态功能结合起来时遇到了问题

以下是我采取的步骤:

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

offcanvas full width

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

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

  

我还有哪些选项可以同时使用具有模态功能的offcanvas菜单?

预要件:

  • Bootstrap.min.css
  • Bootstrap.min.js
  • jasny-bootstrap.css
  • jasny-bootstrap.js

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); 
}

1 个答案:

答案 0 :(得分:2)

不,不应在同一元素上使用模态和非扫描。意外的事情会发生。

如果你只是想用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