twitter-bootstrap 切换到移动屏幕大小时自动关闭 Bootstrap 手风琴面板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18109627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Auto-close Bootstrap accordion panel when switch to mobile screen size
提问by zimbo000
Using Bootstrap 2.3.2 I have an accordion with a single panel that is open when the page is loaded.
使用 Bootstrap 2.3.2 我有一个带有单个面板的手风琴,在页面加载时打开。
<div class="accordion" id="refine">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#refine" href="#refine-search">
Title
</a>
</div>
<div id="refine-search" class="accordion-body collapse in">
<div class="accordion-inner">
Test text....
</div>
</div>
</div>
The site is fully responsive. When switching to a mobile screen size [ @media (max-width: 979px) ] I want the accordion panel to close automatically, i.e. effectively I want the div refine-search line to change to "collapse out".
该网站是完全响应。当切换到移动屏幕尺寸 [@media (max-width: 979px)] 时,我希望手风琴面板自动关闭,即实际上我希望 div 优化搜索行更改为“折叠”。
When in mobile mode, the accordion must still work, e.g. the user can click on the accordion heading and the panel will expand hence I do not want duplicate divs to turn the panel off using .hidden-desktop utility classes etc.
在移动模式下,手风琴必须仍然工作,例如,用户可以单击手风琴标题,面板将展开,因此我不希望重复的 div 使用 .hidden-desktop 实用程序类等关闭面板。
I've searched high and low for an answer - can anyone help?
我在高低处寻找答案 - 任何人都可以帮忙吗?
回答by zimbo000
So I eventually figured out how to do this, posting it in case it's of help to anyone.
所以我最终想出了如何做到这一点,发布它以防它对任何人有帮助。
Alter the HTML to:
将 HTML 更改为:
<div class="accordion" id="refine">
<div class="hidden-phone">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#refine" href="#refine-search">
<legend>
<h2>Refine your search</h2></legend>
</a>
</div>
</div>
<div class="visible-phone">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#refine" href="#refine-search">
<legend>
<h2>Refine your search (press to reveal)</h2></legend>
</a>
</div>
</div>
<div id="refine-search" class="accordion-body collapse in">
<div class="accordion-inner">
Test text....
</div>
</div>
</div>
And then use this JS in the file.
然后在文件中使用这个JS。
$(window).bind('resize load', function() {
if ($(this).width() < 767) {
$('#refine-search').removeClass('in');
$('#refine-search').addClass('out');
} else {
$('#refine-search').removeClass('out');
$('#refine-search').addClass('in');
}
});
回答by jasonflaherty
For Bootstrap 3.x this worked great with no change to their example code:
对于 Bootstrap 3.x,这很有效,无需更改其示例代码:
$(window).bind('resize load', function() {
if ($(this).width() < 767) {
$('.collapse').removeClass('in');
$('.collapse').addClass('out');
} else {
$('.collapse').removeClass('out');
$('.collapse').addClass('in');
}
});
回答by BV2005
Bootstrap 4 accordion:
Bootstrap 4 手风琴:
$(window).bind('resize load', function() {
if ($(this).width() < 767) {
$('.collapse').addClass('show');
} else {
$('.collapse').removeClass('show');
}
});
回答by sshel207
For anyone who comes across this thread: As of 2/17/17, when I came across this thread, both accordion panels were displayed on Desktop and Mobile. The classes "hidden-phone" and "visible-phone" were not showing/hiding depending on the viewport size. I added "hidden-xs" and "visible-xs" to the corresponding divs that wrap the accordion headings and I believe it is now functioning as it was intended to 3 years ago.
对于遇到此线程的任何人:截至 2017 年 2 月 17 日,当我遇到此线程时,桌面和移动设备上都显示了两个手风琴面板。根据视口大小,“隐藏电话”和“可见电话”类未显示/隐藏。我将“hidden-xs”和“visible-xs”添加到包装手风琴标题的相应div中,我相信它现在可以像3年前那样运行。
As rtpHarry explained, you do not need to add and remove the class "out", so I removed it. Other than that I did not make any additional edits.
正如 rtpHarry 解释的那样,您不需要添加和删除“out”类,因此我将其删除。除此之外,我没有做任何额外的编辑。
$(window).bind('resize load', function() {
if ($(this).width() <= 767) {
$('#refine-search').removeClass('in');
} else {
$('#refine-search').addClass('in');
}
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="accordion" id="refine">
<div class="hidden-phone hidden-xs">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#refine" href="#refine-search">
<legend>
<h2>Desktop: Refine your search</h2>
</legend>
</a>
</div>
</div>
<div class="visible-phone visible-xs">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#refine" href="#refine-search">
<legend>
<h2>Mobile: Refine your search (press to reveal)</h2>
</legend>
</a>
</div>
</div>
<div id="refine-search" class="accordion-body collapse in">
<div class="accordion-inner">
Test text....
</div>
</div>
</div>
My example: http://www.bootply.com/ZAahtL5zGp

