gpt4 book ai didi

jquery - 在一定的浏览器大小后删除模态背景(jasny bootstrap)

转载 作者:太空狗 更新时间:2023-10-29 13:30:26 24 4
gpt4 key购买 nike

我正在使用带有 anchor 链接的 jasny bootstrap Canvas 外菜单,以便在单击时导航到 1 月部分。当我点击 anchor 链接时,我添加了一个 jquery 解决方案来关闭 Canvas 外菜单。

JQUERY:关闭 Canvas 外菜单并导航到 anchor 链接区域代码:

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

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

Bonus: 当我选择 992px 宽度后的 anchor 链接时,菜单似乎也会跳转,当 off canvas 菜单变成固定的左侧菜单时,如何防止它跳转?

Fiddle of issue

先决条件:

  • 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}

最佳答案

我对你的 fiddle 做了一些修改,现在你可以查看了 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');
});

关于jquery - 在一定的浏览器大小后删除模态背景(jasny bootstrap),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41895243/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com