Is there a way how can I automatically collapse panel in bootstrap 3.0 on small device? I would like to achieve same effect as top navigation - so when screen is small only a toggle button shows on screen.
To create an accordion that is collapsed by default, we need to set the 'active' property of the jQuery Accordion as false. Syntax: $("#demoAccordion"). accordion({ collapsible: true, active: false});
If you'd like it to default open, add the additional class show . To add accordion-like group management to a collapsible area, add the data attribute data-parent="#selector" .
simple collapsible accordion, with all collapsibles in closed state on page load. all collapsibles can be closed with one click on a button.
To control (show/hide) the collapsible content, add the data-toggle="collapse" attribute to an <a> or a <button> element. Then add the data-target="#id" attribute to connect the button with the collapsible content (<div id="demo">).
This is what I do.
$(document).ready(function(){
if ($(window).width() <= 480){
$('.panel-collapse').removeClass('in');
}
});
$(window).resize(function(){
if ($(window).width() >= 480){
$('.panel-collapse').addClass('in');
}
if ($(window).width() <= 480){
$('.panel-collapse').removeClass('in');
}
});
The currently accepted answer makes an assumption about the break point size being 480. Since the break points can be changed via Less or Sass, a better solution is to figure out the breakpoint in JavaScript.
Bootstrap (surprisingly) doesn't have a built-in utility for doing that, but you can make one yourself easily enough by adding some test divs and checking their visibility. Example below:
var isBreakpoint = function(viewport_size){
return $('.device-' + viewport_size).is(':visible')
}
$(document).ready(function(){
var test_divs = "<div class='device-xs visible-xs'></div>"
test_divs = test_divs + "<div class='device-sm visible-sm'></div>"
test_divs = test_divs + "<div class='device-md visible-md'></div>"
test_divs = test_divs + "<div class='device-lg visible-lg'></div>"
$("body").append(test_divs)
if (isBreakpoint('xs'){
$('.panel-collapse').removeClass('in');
}
else {
$('.panel-collapse').addClass('in');
}
})
Adapted from: How to detect responsive breakpoints of Twitter Bootstrap 3 using JavaScript?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With