删除特定浏览器尺寸后的模态背景 (jasny bootstrap)

Remove modal backdrop after certain browser size (jasny bootstrap)

我正在使用带有锚点 links 的 jasny bootstrap off-canvas 菜单,以便在单击时导航到一月部分。我添加了一个 jquery 解决方案来在单击锚点 link 时关闭 off-canvas 菜单。

JQUERY:关闭 Off-Canvas 菜单并导航到锚 Link 区号:

$('.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.

当我点击 992px 宽度后的锚 link 时,如何防止背景出现?

奖金:当我select一个锚点link在992px宽度后,菜单似乎也会跳转,我怎样才能防止它在off canvas 菜单变成固定的左边菜单?

Fiddle of issue

先决条件:

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}

我对你的 fiddle 做了一些修改,现在你可以检查一下 here。我想我解决了它,现在它正在做你想要的。

  • 如果window 宽度大于 992,导航点击叠加将不会显示
  • 如果 window 宽度小于或等于 992,它将正常工作

请在此处查看 jQuery 代码:

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

您可以通过 CSS 单独使用媒体查询和 !important 强制特定于 jquery 插件的内联样式

来解决这个问题
@media (min-width: 992px) {
  .backdrop {
    display: none!important;
  }
}