我正在使用带有链接链接的jasny bootstrap off-canvas菜单,以便浏览到点击的一月部分.当我点击锚点链接时,我添加了一个
jquery解决方案来关闭帆布外菜单.
$('.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宽度上点击锚链接后,可以做什么来防止背景出现?
奖金:当我在992px宽度之后选择一个锚点链接时,菜单也似乎跳了起来,当关闭画布菜单变成固定的左侧菜单时,如何防止跳转?
先决条件:
> 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() {}); $("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">×</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.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}
解决方法
我在你的小提琴中做了一些改变,现在你可以查看它
here.我想我已经解决了,现在它正在做你想要的.
>如果窗口宽度大于992,导航点击覆盖不会显示
>如果窗口宽度小于或等于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'); });