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

后端 未结 19 2310
失恋的感觉
失恋的感觉 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:05

    For Bootstrup 3.2 + FontAwesome

    $(document).ready(function(){    
        $('#accordProfile').on('shown.bs.collapse', function () {
           $(".fa-chevron-down").removeClass("fa-chevron-down").addClass("fa-chevron-up");
        });
    
        $('#accordProfile').on('hidden.bs.collapse', function () {
           $(".fa-chevron-up").removeClass("fa-chevron-up").addClass("fa-chevron-down");
        });
    });
    

    Sometimes you have to write so. If so

    $('.collapsed span').removeClass('fa-minus').addClass('fa-plus');
    

    Automatically generated (class="collapsed"), when you press the hide menu.

    ps when you need to create a tree menu

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

    If anyone is interested this is how you do it with BS3 since they changed the event names:

    $('.collapse').on('show.bs.collapse', function(){
      var i = $(this).parent().find('i')
      i.toggleClass('fa-caret-right fa-caret-down');
    }).on('hide.bs.collapse', function(){
      var i = $(this).parent().find('i')
      i.toggleClass('fa-caret-down fa-caret-right');
    });
    

    You simply change the class names in the example to the ones you use in your case.

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

    Here is a solution that creates a section that is expandable using somewhat material design, bootstrap 4.5/5 alpha and entirely non-javascript.

    Style for head section

    <style>
    [data-toggle="collapse"] .fa:before {
        content: "\f077";
    }
    
    [data-toggle="collapse"].collapsed .fa:before {
        content: "\f078";
    }
    </style>
    

    Body html

    <div class="pt-3 pb-3" style="border-top: 1px solid #eeeeee; border-bottom: 1px solid #eeeeee; cursor: pointer;">
    <a href="#expandId" class="text-dark float-right collapsed" data-toggle="collapse" role="button" aria-expanded="false" aria-controls="expandId">
        <i class="fa" aria-hidden="false"></i>
    </a>
    <a href="#expandId" class="text-dark collapsed" data-toggle="collapse" role="button" aria-expanded="false" aria-controls="expandId">Expand Header</a>
    <div class="collapse" id="expandId">
        CONTENT GOES IN HERE
    </div>
    
    0 讨论(0)
  • 2020-12-22 19:11

    Use CSSes pseudo-selector :after using Bootstrap 3's integrated Glyphicons for a no JS answer with minor modification to your HTML...

    CSS

    .panel-heading [data-toggle="collapse"]:after
    {
        font-family: 'Glyphicons Halflings';
        content: "\e072"; /* "play" icon */
        float: right;
        color: #b0c5d8;
        font-size: 18px;
        line-height: 22px;
    
        /* rotate "play" icon from > (right arrow) to down arrow */
        -webkit-transform: rotate(-90deg);
        -moz-transform:    rotate(-90deg);
        -ms-transform:     rotate(-90deg);
        -o-transform:      rotate(-90deg);
        transform:         rotate(-90deg);
    }
    .panel-heading [data-toggle="collapse"].collapsed:after
    {
        /* rotate "play" icon from > (right arrow) to ^ (up arrow) */
        -webkit-transform: rotate(90deg);
        -moz-transform:    rotate(90deg);
        -ms-transform:     rotate(90deg);
        -o-transform:      rotate(90deg);
        transform:         rotate(90deg);
    }
    

    HTML

    Add class="collapsed" to any anchor tags that are closed by default.

    This will turn anchors such as

    <a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">
    

    into

    <a class="collapsed" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">
    

    CodePen Live Example

    http://codepen.io/anon/pen/VYobER

    Screenshot

    How it appears in JSBin

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

    A bootstrap v3 solution (where the events have different names), also using only one jQuery selector (as seen here):

    $('.accordion').on('hide.bs.collapse show.bs.collapse', function (n) {
        $(n.target).siblings('.panel-heading').find('i.glyphicon').toggleClass('glyphicon-chevron-down glyphicon-chevron-up');
    });
    
    0 讨论(0)
  • 2020-12-22 19:13

    Added to @muffls answer so that this works with all twitter bootstrap collapse and makes the change to the arrow before the animatation starts.

    $('.collapse').on('show', function(){
        $(this).parent().find(".icon-chevron-left").removeClass("icon-chevron-left").addClass("icon-chevron-down");
    }).on('hide', function(){
        $(this).parent().find(".icon-chevron-down").removeClass("icon-chevron-down").addClass("icon-chevron-left");
    });
    

    Depending on your HTML structure you may need to modify the parent().

    0 讨论(0)
提交回复
热议问题