gpt4 book ai didi

javascript - 通过单击外部关闭 Bootstrap offcanvas 侧边栏

转载 作者:行者123 更新时间:2023-11-28 05:10:23 34 4
gpt4 key购买 nike

我正在尝试修改 Boostrap Offcanvas 模板。

工作示例是 http://getbootstrap.com/examples/offcanvas/ :

它非常适合我,我唯一想添加的是通过点击/点击它的外部来关闭侧边栏菜单(现在它只能通过点击切换按钮来工作)。

非常感谢您的帮助。

html

<div class="container">
<div class="row row-offcanvas row-offcanvas-right">
<div class="col-xs-12 col-sm-9">
<p class="pull-right visible-xs">
<button type="button" class="btn btn-primary btn-xs" data-toggle="offcanvas">Toggle nav</button>
</p>
<div class="row">
<div class="col-xs-6 col-lg-4">
<h2>Heading</h2>
<p>Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui. </p>
<p><a class="btn btn-default" href="#" role="button">View details &raquo;</a></p>
</div>
<!--/.col-xs-6.col-lg-4-->
<div class="col-xs-6 col-lg-4">
<h2>Heading</h2>
<p>Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui. </p>
<p><a class="btn btn-default" href="#" role="button">View details &raquo;</a></p>
</div>
<!--/.col-xs-6.col-lg-4-->
</div>
<!--/row-->
</div>
<!--/.col-xs-12.col-sm-9-->

<div class="col-xs-6 col-sm-3 sidebar-offcanvas" id="sidebar">
<div class="list-group"> <a href="#" class="list-group-item active">Link</a> <a href="#" class="list-group-item">Link</a> <a href="#" class="list-group-item">Link</a> </div>
</div>
<!--/.sidebar-offcanvas-->
</div>
<!--/row-->

CSS

@media screen and (max-width: 767px) {
.row-offcanvas {
position: relative;
-webkit-transition: all .25s ease-out;
-o-transition: all .25s ease-out;
transition: all .25s ease-out;
}

.row-offcanvas-right {
right: 0;
}


.row-offcanvas-right
.sidebar-offcanvas {
right: -50%; /* 6 columns */
}


.row-offcanvas-right.active {
right: 50%; /* 6 columns */
}

.sidebar-offcanvas {
position: absolute;
top: 0;
width: 50%; /* 6 columns */
}
}

jQuery

$(document).ready(function () {
$('[data-toggle="offcanvas"]').click(function () {
$('.row-offcanvas').toggleClass('active')
});
});

最佳答案

你可以尝试这样的事情:

$(document).click(function(e) {
var target = e.target;
if (!$(target).is('#sidebar') && !$(target).parents().is('#sidebar')) {
$('.row-offcanvas').removeClass('active')
}
});

关于javascript - 通过单击外部关闭 Bootstrap offcanvas 侧边栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39593870/

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