Adding open/closed icon to Twitter Bootstrap collapsibles (accordions)

后端 未结 19 2312
失恋的感觉
失恋的感觉 2020-12-22 18:23

I\'ve set up this simple version of the Bootstrap accordion:

Simple accordion: http://jsfiddle.net/darrenc/cngPS/

Currently the icon only po

相关标签:
19条回答
  • 2020-12-22 19:14

    Here it's the answer for those who are looking for a solution in Bootstrap 3(like myself).

    The HTML part:

    <div class="btn-group">
      <button type="button" class="btn btn-danger">Action</button>
      <button type="button" class="btn btn-danger" data-toggle="collapse" data-target="#demo">
        <span class="glyphicon glyphicon-minus"></span>
      </button>
    </div>
    <div id="demo" class="collapse in">Some dummy text in here.</div>
    

    The js part:

    $('.collapse').on('shown.bs.collapse', function(){
    $(this).parent().find(".glyphicon-plus").removeClass("glyphicon-plus").addClass("glyphicon-minus");
    }).on('hidden.bs.collapse', function(){
    $(this).parent().find(".glyphicon-minus").removeClass("glyphicon-minus").addClass("glyphicon-plus");
    });
    

    Example accordion:

    Bootply accordion example

    0 讨论(0)
  • 2020-12-22 19:14

    @RobSadler:

    RE Martin Wickman's CSS only version...

    You can get around that problem by putting accordion-caret on the anchor tag and giving it a collapsed class by default. Like so:

    <div class="accordion-group">
    <div class="accordion-heading">
        <a class="accordion-toggle accordion-caret collapsed" data-toggle="collapse" href="#collapseOne">
            <strong>Header</strong>
        </a>
    </div>
    <div id="collapseOne" class="accordion-body collapse in">
        <div class="accordion-inner">
            Content
        </div>
    </div>
    

    That worked for me.

    0 讨论(0)
  • 2020-12-22 19:15

    For a CSS-only (and icon-free) solution using Bootstrap 3 I had to do a bit of fiddling based on Martin Wickman's answer above.

    I didn't use the accordion-* notation because it's done with panels in BS3.

    Also, I had to include in the initial HTML aria-expanded="true" on the item that's open at page load.

    Here is the CSS I used.

    .accordion-toggle:hover { text-decoration: none; }
    .accordion-toggle:hover span, .accordion-toggle:hover strong { text-decoration: underline; }
    .accordion-toggle:before { font-size: 25px; }
    .accordion-toggle[data-toggle="collapse"]:before { content: "+"; margin-right: 0px; }
    .accordion-toggle[aria-expanded="true"]:before { content: "-"; margin-right: 0px; }
    

    Here is my sanitized HTML:

    <div id="acc1">    
        <div class="panel panel-default">
            <div class="panel-heading">
                <span class="panel-title">
                    <a class="accordion-toggle" data-toggle="collapse" aria-expanded="true" data-parent="#acc1" href="#acc1-1">Title 1
                        </a>
                </span>
            </div>
            <div id=“acc1-1” class="panel-collapse collapse in">
                <div class="panel-body">
                    Text 1
                </div>
            </div>
        </div>
        <div class="panel panel-default">
            <div class="panel-heading">
                <span class="panel-title">
                    <a class="accordion-toggle" data-toggle="collapse" data-parent="#acc1” href=“#acc1-2”>Title 2
                        </a>
                </span>
            </div>
            <div id=“acc1-2” class="panel-collapse collapse">
                <div class="panel-body">
                    Text 2                  
                </div>
            </div>
        </div>
    </div>
    
    0 讨论(0)
  • 2020-12-22 19:16

    This is how I do it without any js.

    I used the icon glyphicon-triangle-right but it works with any other icon, what it does is that it applies a 90 degrees rotation to the icon when the panel is open or not. I'm using Bootstrap 3.3.5 for this one.

    CSS Code

    h4.panel-title a {
        display: block;
    
    }
    h4.panel-title a.collapsed .glyphicon-triangle-right {
            color: #ada9a9 !important;
            transform: rotate(0deg);
    }
    h4.panel-title a .glyphicon-triangle-right {
            color: #515e64 !important;
            transform: rotate(90deg);
    }
    

    This is the HTML structure taken from the Bootstrap example

    <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
                                <div class="panel panel-default">
                                    <div class="panel-heading" role="tab" id="headingOne">
                                        <h4 class="panel-title">
                                            <a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
                                                Proven Expertise
                                                <span class="glyphicon glyphicon-triangle-right pull-right" aria-hidden="true"></span>
                                            </a>
                                        </h4>
                                    </div>
                                    <div id="collapseOne" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne">
                                        <div class="panel-body">
                                            Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
                                        </div>
                                    </div>
                                </div>
    
                            </div>
    
    0 讨论(0)
  • 2020-12-22 19:17

    The Bootstrap Collapse has some Events that you can react to:

    $(document).ready(function(){    
        $('#accordProfile').on('shown', function () {
           $(".icon-chevron-down").removeClass("icon-chevron-down").addClass("icon-chevron-up");
        });
    
        $('#accordProfile').on('hidden', function () {
           $(".icon-chevron-up").removeClass("icon-chevron-up").addClass("icon-chevron-down");
        });
    });
    
    0 讨论(0)
  • 2020-12-22 19:19

    None of the above worked for me but I came up with this and it worked:

    function toggleChevron(el) {
      if ($(el).find('i').hasClass('icon-chevron-left'))
          $(el).find('.icon-chevron-left').removeClass("icon-chevron-left").addClass("icon-chevron-down");
      else 
          $(el).find('.icon-chevron-down').removeClass("icon-chevron-down").addClass("icon-chevron-left");
    }
    

    HTML implementation:

    <div class="accordion" id="accordion-send">
      <div class="accordion-group">
        <div class="accordion-heading" onClick="toggleChevron(this)">
          <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion-send" href="#collapse-refund">
            <i class="icon icon-chevron-right"></i> Send notice
          </a>
          ...
    
    0 讨论(0)
提交回复
热议问题