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

后端 未结 19 2309
失恋的感觉
失恋的感觉 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 18:54

    Another no-javascript solution that uses the collapse functionality itself:

    /* Prevent block display and transition animation */
    .expand-icon.collapse.in,
    .collapse-icon.collapse.in {
      display: inline; }
    .expand-icon.collapsing {
      display: none; }
    
    /* HTML Toggler with glyphicons */
    <a data-toggle="collapse" href=".my-collapse">
     <span class="my-collapse collapse-icon collapse in">
      <span class="glyphicon glyphicon-collapse-up"></span>
     </span>
     <span class="my-collapse expand-icon collapse">
      <span class="glyphicon glyphicon-expand"></span>   
     </span>
    </a>
    
    0 讨论(0)
  • 2020-12-22 18:55

    Here is my solution which is further refined from one posted by @john-magnolia and solves some of its issues

    /**
     * Toggle on/off arrow for Twitter Bootstrap collapsibles.
     *
     * Multi-collapsible-friendly; supports several collapsibles in the same group, on the same page.
     */
    function animateCollapsibles() {
    
        $('.collapse').on('show', function() {
            var $t = $(this);
            var header = $("a[href='#" + $t.attr("id") + "']");
            header.find(".icon-chevron-right").removeClass("icon-chevron-right").addClass("icon-chevron-down");
        }).on('hide', function(){
            var $t = $(this);
            var header = $("a[href='#" + $t.attr("id") + "']");
            header.find(".icon-chevron-down").removeClass("icon-chevron-down").addClass("icon-chevron-right");
        });
    }
    

    And here is the example markup:

    <div class="accordion" id="accordion-send">
        <div class="accordion-group">
            <div class="accordion-heading">
                <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion-send" href="#collapse-refund">
                    <i class="icon icon-chevron-right"></i> Send notice
                </a>
            </div>
            <div id="collapse-refund" class="accordion-body collapse">
                <div class="accordion-inner">
                    <p>Lorem ipsum Toholampi city</p>
                </div>
            </div>
        </div>
    </div>
    
    0 讨论(0)
  • 2020-12-22 18:56

    The most readable CSS-only solution would probably be to use the aria-expanded attribute. Remember that you'll need to add aria-expanded="false" to all collapse-elements as this is not set on load (only on first click).

    The HTML:

    <h2 data-toggle="collapse" href="#collapseId" aria-expanded="false">
        <span class="glyphicon glyphicon-chevron-right"></span>
        <span class="glyphicon glyphicon-chevron-down"></span> Title
    </h2>
    

    The CSS:

    h2[aria-expanded="false"] span.glyphicon-chevron-down,
    h2[aria-expanded="true"] span.glyphicon-chevron-right
    {
        display: none;
    }
    
    h2[aria-expanded="true"] span.glyphicon-chevron-down,
    h2[aria-expanded="false"] span.glyphicon-chevron-right
    {
        display: inline;
    }
    

    Works with Bootstrap 3.x.

    0 讨论(0)
  • 2020-12-22 18:58

    I think the best codes are these:

      $('#accordion1').collapse({
        toggle: false
      }).on('show',function (e) {
            $(e.target).parent().find(".icon-chevron-down").removeClass("icon-chevron-down").addClass("icon-chevron-up");
          }).on('hide', function (e) {
            $(e.target).parent().find(".icon-chevron-up").removeClass("icon-chevron-up").addClass("icon-chevron-down");
          });
    
    0 讨论(0)
  • 2020-12-22 19:00

    This has been answered by numerous ways but what I came up with was the simplest and easiest for me with Bootstrap 3 and font awesome. I just did

    $('a.toggler').on('click', function () {$('i', this).toggleClass('fa-caret-up');});
    

    This just toggles the CSS class of the icon I want to show. I add the class toggler to the item I want to apply this to. This can be added onto any item you want to toggle an icon.

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

    Here is my approach for Bootstrap 2.x. It is just some css. No JavaScript needed:

        .accordion-caret .accordion-toggle:hover {
            text-decoration: none;
        }
        .accordion-caret .accordion-toggle:hover span,
        .accordion-caret .accordion-toggle:hover strong {
            text-decoration: underline;
        }
        .accordion-caret .accordion-toggle:before {
            font-size: 25px;
            vertical-align: -3px;
        }
        .accordion-caret .accordion-toggle:not(.collapsed):before {
            content: "▾";
            margin-right: 0px;
        }
        .accordion-caret .accordion-toggle.collapsed:before {
            content: "▸";
            margin-right: 0px;
        }
    

    Just add class accordion-caret to the accordion-group div, like this:

    <div class="accordion-group accordion-caret">
        <div class="accordion-heading">
            <a class="accordion-toggle" 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>
    </div>
    
    0 讨论(0)
提交回复
热议问题