ChosenJuan
ChosenJuan

Reputation: 961

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

I'm having trouble combining off-canvas with modal functionality using Jasny Bootstrap

These are the steps I took:

1. I created an off-canvas menu using jasny bootstrap, and it works as intended Off Canvas Only

offcanvas full width

2. However, when I add the modal functionality, it makes the off-canvas menu disappear after stretching the screen to 992px width. Also I can't close the modal by clicking outside of the element. Modal functionality + Off-Canvas Modal small screen

The problem I have is with the data-toggle="modal" or data-toggle="offcanvas" I can only use one or the other. Is there another way to add two data toggles? Or better yet:

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

Pre-Requisites:

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

Upvotes: 0

Views: 3324

Answers (1)

Arnold Daniels
Arnold Daniels

Reputation: 16583

No, modal and offcanvas should not be used on the same element. Unexpected things will happen.

If you simply want to add a backdrop with offcanvas, better add that manually rather than using the modal plugin.

In the HTML add

<div class="backdrop">

With CSS

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

Than using JavaScript, show and hide the backdrop

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

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

Last, adding a close button to the menu is trivial

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

See the fiddle

Upvotes: 2

Related Questions