By default, the boot lock bar is closed

I am using a self-adjusting panel and I have added a navigable icon: plus when the body panel is open and minus when the panel body is close, my problem now is how can I do to set it to default (plus icon, shown by default and when I click on it the panel will open)

above my code to do this, thanks in advance for your help.

<html>

<head>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script>
$(document).on('click', '.panel-heading span.clickable', function (e) {
    var $this = $(this);
    if (!$this.hasClass('panel-collapsed')) {

        $this.parents('.panel').find('.panel-body').slideUp();
        $this.addClass('panel-collapsed');
        $this.find('i').removeClass('glyphicon-minus').addClass('glyphicon-plus');
    } else {
    console.log($this);

        $this.parents('.panel').find('.panel-body').slideDown();
        $this.removeClass('panel-collapsed');
        $this.find('i').removeClass('glyphicon-plus').addClass('glyphicon-minus');
    }
});
</script>
<style>

.clickable
{
    cursor: pointer;
}

.clickable .glyphicon
{
    background: rgba(0, 0, 0, 0.15);
    display: inline-block;
    padding: 6px 12px;
    border-radius: 4px
}

.panel-heading span
{
    margin-top: -23px;
    font-size: 15px;
    margin-right: -9px;
}
</style>
</head>
 <body>
 <div class="container">
    <div class="row">
        <div class="col-md-6">
            <div class="panel panel-primary">
                <div class="panel-heading">
                    <h3 class="panel-title">
                        Panel 1</h3>
                    <span class="pull-right clickable panel-collapsed in"><i class="glyphicon glyphicon-plus"></i></span>
                </div>
                <div class="panel-body">
                    Panel content</div>
            </div>
        </div>

        </div>
    </div>
 </body>

</html>

      

+3


source to share


3 answers


You just need to change this:

<div class="panel-body">

      

For this:

<div class="panel-body"  style="display:none;">

      

And change slideUp () to slideDown (), and same for slideDown () with slideUp ().

Also change this:



<span class="pull-right clickable collapse in"><i class="glyphicon glyphicon-plus"></i></span>

      

For this:

<span class="pull-right clickable collapse in"><i class="glyphicon glyphicon-minus"></i></span>

      

Everything should work as you mentioned in your question now.

Here's a JSFiddle link: Try it

+3


source


You can use the Bootstrap collapse component which already has most of the required logic:

<div class="panel-heading">
   <h3 class="panel-title">Panel 1</h3>
   <button class="btn btn-primary pull-right" type="button"
           data-toggle="collapse" data-target="#collapseExample">
      <i class="glyphicon glyphicon-plus"></i>
    </button>
</div>
<div class="collapse" id="collapseExample">
    <div class="panel-body">Panel content</div>
</div>

      

Also, only a little JavaScript is needed to change the button icon:

    $(document).on('click', '.panel-heading button', function(e) {
      var $this = $(this);
      var icon = $this.find('i');
      if (icon.hasClass('glyphicon-plus')) {
        $this.find('i').removeClass('glyphicon-plus').addClass('glyphicon-minus');
      } else {
        $this.find('i').removeClass('glyphicon-minus').addClass('glyphicon-plus');
      }
    });

      



Working example:

<html>

<head>
  <!-- Latest compiled and minified CSS -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
  <!-- Optional theme -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  <!-- Latest compiled and minified JavaScript -->
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
  <script>
    $(document).on('click', '.panel-heading button', function(e) {
      var $this = $(this);
      var icon = $this.find('i');
      if (icon.hasClass('glyphicon-plus')) {
        $this.find('i').removeClass('glyphicon-plus').addClass('glyphicon-minus');
      } else {
        $this.find('i').removeClass('glyphicon-minus').addClass('glyphicon-plus');
      }
    });
  </script>
  <style>
    .clickable {
      cursor: pointer;
    }
    .clickable .glyphicon {
      background: rgba(0, 0, 0, 0.15);
      display: inline-block;
      padding: 6px 12px;
      border-radius: 4px
    }
    .panel-heading span {
      margin-top: -23px;
      font-size: 15px;
      margin-right: -9px;
    }
    .panel-heading button  {
      margin-top: -25px;
    }
  </style>
</head>

<body>
  <div class="container">
    <div class="row">
      <div class="col-md-6">
        <div class="panel panel-primary">
          <div class="panel-heading">
            <h3 class="panel-title">
                        Panel 1</h3>
            <button class="btn btn-primary pull-right" type="button" data-toggle="collapse" data-target="#collapseExample" aria-expanded="false" aria-controls="collapseExample">
              <i class="glyphicon glyphicon-plus"></i>
            </button>
          </div>
          <div class="collapse" id="collapseExample">
            <div class="panel-body">
              Panel content</div>
          </div>
        </div>

      </div>

    </div>
  </div>
</body>

</html>
      

Run codeHide result


+2


source


I am using the reference code for you, just follow the code, no need for any click function, bootstrap 'data-toggle = "collapse"' handles it automatically.

CODE:

            <div class="panel-group" id="steps">
                <!-- Step 1 -->
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h4 class="panel-title">
                            <a data-toggle="collapse" data-parent="#steps" href="#stepOne"><SPAN TAG></a>
                        </h4>
                    </div>
                    <div id="stepOne" class="panel-collapse collapse">
                        <div class="panel-body">
                            <div class="form-group">
                                <label class="col-lg-2 control-label">Sub-Category Name</label>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

      

Just put a span tag instead and use the pull-right class, the bootstrap icon you need http://getbootstrap.com/components/

I hope this works.

0


source







All Articles