Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collapsing Sidebar with Bootstrap

I just visited this page http://www.elmastudio.de/ and wondered if it is possible to build the left sidebar collapse with Bootstrap 3.

Code to collapse the sidebar from the top (phone only):

<button type="button" class="navbar-toggle sidebar-toggle" data-toggle="collapse" data-target=".sidebar-ex1-collapse">     <span class="sr-only">Sidebar</span>     <span class="glyphicon glyphicon-check"></span> </button> 

Sidebar itself has the hidden-xs class. It is removed with the following js

$('.sidebar-toggle').click(function(){      $('#sidebar').removeClass('hidden-xs');             }); 

If you click the button it toggles the sidebar from the top. It be great to see the sidebar behave like the website above shows. Any help is appreciated!

like image 914
Chris We Avatar asked Oct 16 '13 07:10

Chris We


People also ask

How do I make my navbar collapse?

To create a collapsible navigation bar, use a button with class="navbar-toggler", data-toggle="collapse" and data-target="#thetarget" . Then wrap the navbar content (links, etc) inside a div element with class="collapse navbar-collapse" , followed by an id that matches the data-target of the button: "thetarget".

Does bootstrap have a sidebar?

Basic exampleOfficial Bootstrap documentation does not contain a Sidebar component, but it's possible to create fully-functional side navigation from the existing components, and with the little help of Material Design for Bootstrap - free and powerfull UI KIT.

How does Bootstrap collapse work?

Bootstrap collapse is used to show or hide content. Buttons or anchors are used to trigger the request and they are mapped to the specific element that needs to be collapsed. In Bootstrap, the collapsing element animates the height of the element from its current height to 0.


2 Answers

Bootstrap 3

Yes, it's possible. This "off-canvas" example should help to get you started.

https://codeply.com/p/esYgHWB2zJ

Basically you need to wrap the layout in an outer div, and use media queries to toggle the layout on smaller screens.

/* collapsed sidebar styles */ @media screen and (max-width: 767px) {   .row-offcanvas {     position: relative;     -webkit-transition: all 0.25s ease-out;     -moz-transition: all 0.25s ease-out;     transition: all 0.25s ease-out;   }   .row-offcanvas-right   .sidebar-offcanvas {     right: -41.6%;   }    .row-offcanvas-left   .sidebar-offcanvas {     left: -41.6%;   }   .row-offcanvas-right.active {     right: 41.6%;   }   .row-offcanvas-left.active {     left: 41.6%;   }   .sidebar-offcanvas {     position: absolute;     top: 0;     width: 41.6%;   }   #sidebar {     padding-top:0;   } } 

Also, there are several more Bootstrap sidebar examples here


Bootstrap 4

Create a responsive navbar sidebar "drawer" in Bootstrap 4?

like image 57
Zim Avatar answered Sep 27 '22 22:09

Zim


http://getbootstrap.com/examples/offcanvas/

This is the official example, may be better for some. It is under their Experiments examples section, but since it is official, it should be kept up to date with the current bootstrap release.

Looks like they have added an off canvas css file used in their example:

http://getbootstrap.com/examples/offcanvas/offcanvas.css

And some JS code:

$(document).ready(function () {   $('[data-toggle="offcanvas"]').click(function () {     $('.row-offcanvas').toggleClass('active')   }); }); 
like image 40
Jon Avatar answered Sep 27 '22 22:09

Jon