Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically collapse panel on small device in bootstrap?

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.

like image 982
Piotr Stapp Avatar asked Sep 12 '13 14:09

Piotr Stapp


People also ask

How do you collapse an accordion by default?

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});

How do I make accordion open by default in Bootstrap?

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" .

How do I close a Bootstrap collapse?

simple collapsible accordion, with all collapsibles in closed state on page load. all collapsibles can be closed with one click on a button.

How do you expand and collapse in Bootstrap?

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">).


2 Answers

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');
  }
});
like image 157
Ken Ratanachai S. Avatar answered Oct 20 '22 14:10

Ken Ratanachai S.


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?

like image 35
Scribblemacher Avatar answered Oct 20 '22 16:10

Scribblemacher