Bootstrap 3 collapse change chevron icon on click

前端 未结 12 1824
别那么骄傲
别那么骄傲 2020-12-08 03:01

I have read all related questions regarding my question and have tried them all to no avail. I can\'t seem to make my code work, even though I \"think\" almost every code I

相关标签:
12条回答
  • 2020-12-08 03:29

    you can try this.

    Here's the HTML Code:

    <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="false" aria-controls="collapseOne"> Collapsible Group Item #1<span class="glyphicon glyphicon-chevron-up"></span> </a>
    

    Here's the JQuery

    $('#accordion').on('shown.bs.collapse hidden.bs.collapse', function (e) {
             $(e.target).prev('.panel-heading').find("span.glyphicon").toggleClass('glyphicon-chevron-up glyphicon-chevron-down',200, "easeOutSine" );
    });
    
    0 讨论(0)
  • 2020-12-08 03:30

    Similar to Bojan's answer, I use this solution.
    Change HTML code like this:

    <span class="chevron_toggleable glyphicon glyphicon-chevron-down"></span>
    

    It's better to use .on event with respect to .click. Also by using class selector it can be used as a site wide solution.

    $('.chevron_toggleable').on('click', function() {
        $(this).toggleClass('glyphicon-chevron-down glyphicon-chevron-up');
    });
    
    0 讨论(0)
  • 2020-12-08 03:32

    Pure CSS, with even less code + animation.

    HTML part:

       <a data-toggle="collapse" href="#collapseExample" 
          aria-expanded="false" aria-controls="collapseExample">
            Open/Close collapse
            <i class="fa fa-chevron-right pull-right"></i>
        </a>
    

    CSS:

    a[aria-expanded=true] .fa-chevron-right {
     transition: .3s transform ease-in-out;
     transform: rotate(90deg);
    }
    
    0 讨论(0)
  • 2020-12-08 03:33

    I'd like to offer an option along the same lines as another solution posted here, but uses a single div with transforms. This would also help make clean use of transitions to animate the icons as well.

    `a[aria-expanded=true] .fa-chevron-right { transform: rotate(0deg); }

    a[aria-expanded=false] .fa-chevron-right { transform: rotate(90deg); // or whatever direction you need }`

    0 讨论(0)
  • 2020-12-08 03:33
    <div id="accordion">
      <div class="card">
        <div class="card-header" id="headingOne">
          <h5 class="mb-0">
            <button class="btn btn-link" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
              Collapsible Group Item #1
            </button>
          </h5>
        </div>
    
        <div id="collapseOne" class="collapse show" aria-labelledby="headingOne" data-parent="#accordion">
          <div class="card-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 class="card">
        <div class="card-header" id="headingTwo">
          <h5 class="mb-0">
            <button class="btn btn-link collapsed" data-toggle="collapse" data-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
              Collapsible Group Item #2
            </button>
          </h5>
        </div>
        <div id="collapseTwo" class="collapse" aria-labelledby="headingTwo" data-parent="#accordion">
          <div class="card-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 class="card">
        <div class="card-header" id="headingThree">
          <h5 class="mb-0">
            <button class="btn btn-link collapsed" data-toggle="collapse" data-target="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
              Collapsible Group Item #3
            </button>
          </h5>
        </div>
        <div id="collapseThree" class="collapse" aria-labelledby="headingThree" data-parent="#accordion">
          <div class="card-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>
    
    
    <script>    
    
    $('.card-header').click(function(){
            $cur = $(this);
            setTimeout(function(){ 
                $('.far').removeClass("fa-minus-square").addClass("fa-plus-square");
                if($cur.next().hasClass("show")) {
                    //console.log('show');
                    $cur.find('.far').removeClass("fa-plus-square").addClass("fa-minus-square");
                } else {
                    //console.log('no show');
                    $cur.find('.far').removeClass("fa-minus-square").addClass("fa-plus-square");
                }
            }, 500);
    
    
        });
        </script>
    
    0 讨论(0)
  • 2020-12-08 03:36

    Try this more elegant solution:

    $('#serviceList').click(function(){
        $(this).find('.servicedrop').toggleClass('icon-chevron-down icon-chevron-up');
    });
    
    0 讨论(0)
提交回复
热议问题