Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap panel: collapsed on mobile but expanded on desktop, how?

How can I as simply as possible remove the class "in" from the div with id "panel-login" if the screen are smaller than 1199px (Bootstrap's xs, sm and md modes). I guess some kind of JS or JQuery could solve it but I'm not very good at either.

<div class="panel-group">
<div class="panel panel-default">
    <div class="panel-heading">

        <div class="row">

            <div class="col-xs-10">
                <h4 class="panel-title">Login</h4>
            </div>

            <div class="col-xs-2 text-right">
                <a data-toggle="collapse" href="#panel-login"><i class="fa fa-bars"></i></a>
            </div>

        </div>

    </div>

    <div id="panel-login" class="panel-collapse collapse in">

        <div class="panel-body">

            <ul>
                <li><a href="<?=$php_self?>?page=login&view=signup">Create account</a></li>
                <li><a href="<?=$php_self?>?page=login&view=reset_password">Reset password</a></li>
                <li><a href="<?=$php_self?>?page=login&view=reactivate_account">Reactivate account</a></li>
            </ul>

        </div><!-- end panel-body -->

    </div>

</div><!-- end panel-default -->

like image 791
Torbjörn Loke Nornwen Avatar asked Feb 28 '16 12:02

Torbjörn Loke Nornwen


People also ask

How to expand and collapse elements manually in Bootstrap?

You may also expand and collapse elements manually via JavaScript — just call the collapse () Bootstrap method with the id or class selector of the collapsible element. <script> $ (document).ready (function () { $ ("#myBtn").click (function () { $ ("#myCollapse").collapse ("toggle"); }); }); </script>

How to show/hide the collapsible content with a button click?

Lorem ipsum dolor text.... The .collapse class indicates a collapsible element (a <div> in our example); this is the content that will be shown or hidden with a click of a button. To control (show/hide) the collapsible content, add the data-bs-toggle="collapse" attribute to an <a> or a <button> element.

How do I show and hide elements in Bootstrap?

Buttons and anchors (i.e. the <button> and <a> elements) are typically used as triggers that are mapped to the elements you want to toggle. You can show and hide elements in Bootstrap by clicking on a button or link via data attributes without writing any JavaScript code. Let's try out an example and see how it actually works: <!--


3 Answers

You could use a CSS @media query to hide it.. overriding the effect if the .in

@media (max-width:1199px) {
    #panel-login {
        display:none;
    }
}

CSS: http://codeply.com/go/Q4X15Bp31R

Or, you can use jQuery and watch the resize event like this..

function togglePanel (){
   var w = $(window).width();
   if (w <= 1199) {
      $('#panel-login').removeClass('in');
   } else {
      $('#panel-login').addClass('in');
   }
}

$(window).resize(function(){
     togglePanel();
});

togglePanel();

jQuery: http://codeply.com/go/okQQKcdO7v

like image 67
Zim Avatar answered Oct 19 '22 23:10

Zim


This is what I used:

@media (min-width: 768px) {
  #my-div .collapse {
    display: block;
  }
}
like image 34
createscape Avatar answered Oct 20 '22 00:10

createscape


HTML

In you panel-heading element just add

<span class="pull-right clickable"><i class="glyphicon glyphicon-chevron-down"></i></span>

And in your panel-body element add "collapse":

<div class="panel-body collapse">

JAVASCRIPT

$(document).on('click', '.panel-heading span.clickable', function(e){
    var $this = $(this);
    var d = $this.parents('.panel').find('.panel-body');
        if(!d.hasClass('collapse in')) {
            d.slideDown();
            d.addClass('in');
            $this.find('i').removeClass('glyphicon-chevron-down').addClass('glyphicon-chevron-up');
        }
        else {
            d.slideUp();
            d.removeClass('in');
            $this.find('i').removeClass('glyphicon-chevron-up').addClass('glyphicon-chevron-down');
        }
})

Once you've added the javascript snippet globally you are able to toogle every panel by just adding the HTML snippet.

To make it collapsed for mobile devices just trigger the collapse() function for mobile devices,like

if (($(window).width()) > 767) {
    $('.collapse').collapse();
} 

OR you could check if the hamburger menu is visible

if (!$('.navbar-toggle').is(':visible')) {
  $('.collapse').collapse();
}
like image 30
Stefan Avatar answered Oct 19 '22 23:10

Stefan