Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove modal backdrop after certain browser size (jasny bootstrap)

I'm using the jasny bootstrap off-canvas menu with anchor links in order to navigate to the january section on click. I added a jquery solution to close the off-canvas menu when I click on the anchor link.

JQUERY: Close Off-Canvas Menu and Navigate to Anchor Link area code:

$('.navmenu-nav li a').on('click', function(){
   $(".backdrop").hide( 0, function() {});
    $("body").removeClass("bs.offcanvas");});

Problem The above code works great under 992px browser width when the off canvas menu is active, but when the browser width goes past 992px the off canvas menu becomes a fixed left menu, but the black backdrop still appears when I click on the anchor link. I tried to use the removeAttr data-target and data-toggle after 992px width without success. I'm still very new at JQuery and I can't seem to figure this out.

There should be no black overlay when I click on the anchor link after 992px in width

What can I do to prevent the backdrop from appearing when I click on anchor link after 992px width?

Bonus: The menu also seems to jump when I select an anchor link after 992px width, how can I prevent it from jumping when the off canvas menu becomes a fixed left menu?

Fiddle of issue

Pre-Requisite:

  • Bootstrap.min.css

  • Bootstrap.min.js

  • jasny-bootstrap.css

  • jasny-bootstrap.js

JQuery:

/Close Modal when navigating to anchor   */
$('.navmenu-nav li a').on('click', function(){
   $(".backdrop").hide( 0, function() {});
    $("body").removeClass("bs.offcanvas");

    });
/Simulate Modal Opening */

$(".nav-link").click(function() { $("#navToggle").click() })

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

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


/Close Modal on Resize */
$(window).resize(function() {
  if ($(window).width() > 992) {
    $(".backdrop").hide( 0, function() {});
    $("body").removeClass("bs.offcanvas");

 }
});

HTML

<div class="backdrop"></div>

    <div class="navmenu navmenu-default navmenu-fixed-left offcanvas-sm colornav ">
    <a href="#" class="close" data-toggle="offcanvas" data-target=".navmenu">&times;</a>
     <a id="navToggle" class=""><span></span></a>
      <h4 class="navmenu-brand visible-md visible-lg visible-sm visible-xs" href="#">2017</h4>
      <ul class="nav navmenu-nav">
        <li class="active"><a data-toggle="offcanvas" data-target=".navmenu" class="nav-link" href="#january">Enero</a></li>
        <li><a class="nav-link" href="http://www.jasny.net/bootstrap/examples/navmenu-push/">Msrs</a></li>
        <li><a class="nav-link" href="http://www.jasny.net/bootstrap/examples/navmenu-reveal/">Jupiter</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 navbar-preheader">
      <button type="button" class="navbar-toggle" data-toggle="offcanvas" data-target=".navmenu">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">navbar brand</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>
      <p>Also take a look at the examples for a navmenu with <a href="http://www.jasny.net/bootstrap/examples/navmenu-push">push effect</a> and <a href="http://www.jasny.net/bootstrap/examples/navmenu-reveal">reveal effect</a>.</p>
<p class="space"></p>
      <p id="january">sssssssssssssssssssssssssssssssssssssssssssss</p>
    </div><!-- /.container -->

CSS:

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

.space {padding-bottom:900px;}

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

.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: 30px 0 0 300px;
  }
  .navmenu {
    padding-top: 0; 
  }

.navbar-toggle {display:none!important;}
.close {display:none}


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

}

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


.close {margin-right:10px; margin-top:10px;}

@media (max-width:991px) {



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


}

    .backdrop {display:none}
like image 257
ChosenJuan Avatar asked Jan 27 '17 13:01

ChosenJuan


1 Answers

I've made some changes in your fiddle, now you can check it here. I think I resolved it, and now it's doing exactly what you want.

  • if window width is more than 992, on navigation click overlay won't be shown
  • if window width is less or equal then 992, it will work as it should

See jQuery code here:

// simulate modal opening
$('.nav-link').click(function(e) {
  if ($(window).width() > 992) {
    $('.backdrop').hide(0, false);
  }
  
	$('#navToggle').click();
});

$('.navmenu').on('show.bs.offcanvas', function() {
  if ($(window).width() <= 992) {
    $('.backdrop').fadeIn();
  }
});

$('.navmenu').on('hide.bs.offcanvas', function() {
  if ($(window).width() <= 992) {
    $('.backdrop').fadeOut();
  }
});


// close modal on resize
$(window).resize(function() {
  if ($(window).width() > 992) {
    $('.backdrop').hide(0, false);
    $('body').removeClass('bs.offcanvas');
  }
});

// switch active navigation link onclick
$('.nav a').on('click', function() {
  $('.nav').find('.active').removeClass('active');
  $(this).parent().addClass('active');
});

// close Modal when navigating to anchor
$('.navmenu-nav li a').on('click', function() {
  $('.backdrop').hide(0, false);
  $('body').removeClass('bs.offcanvas');
});
like image 157
zgabievi Avatar answered Oct 24 '22 02:10

zgabievi