Bootstrap 3 collapse change chevron icon on click

前端 未结 12 1823
别那么骄傲
别那么骄傲 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:10

    For change collapse icon in Bootstrap 4 with minimal html change, I've done

    • Add to css:

      a[data-toggle="collapse"]:after {
          font-family: 'Glyphicons Halflings';
          content: "\e114";
          float: right;
          color: #4285F4;
      }
      a[data-toggle="collapse"].collapsed:after {
          content: "\e080";
      }
      @font-face {
          font-family: 'Glyphicons Halflings';
          src: url('../fonts/glyphicons-halflings-regular.eot');
          src: url('../fonts/glyphicons-halflings-regular.eot?#iefix') format('embedded-opentype'),
          url('../fonts/glyphicons-halflings-regular.woff2') format('woff2'),
          url('../fonts/glyphicons-halflings-regular.woff') format('woff'),
          url('../fonts/glyphicons-halflings-regular.ttf') format('truetype'),
          url('../fonts/glyphicons-halflings-regular.svg#glyphicons_halflingsregular') format('svg');
      }
      
    • Put fonts in proper place, related to css:

      \css\style.css
      \fonts\glyphicons-halflings-regular.eot
      \fonts\glyphicons-halflings-regular.svg
      \fonts\glyphicons-halflings-regular.ttf
      \fonts\glyphicons-halflings-regular.woff
      \fonts\glyphicons-halflings-regular.woff2
      
    • And add class="collapsed" to all collapsed (by default) links:

      <a href="#info" data-toggle="collapse" class="collapsed">Collapsed link</a>
      <div id="info" class="collapse">
      
    0 讨论(0)
  • 2020-12-08 03:15

    The problem is with your jQuery code not being correct.

    You're closing the event handler function early on this line:

    $('#serviceList').on('shown.bs.collapse'), function() {
    

    See that second closing parenthesis? That's closing the 'on' function early. Try changing your jQuery to look like this:

    $('#serviceList').on('shown.bs.collapse', function() {
        $(".servicedrop").addClass('glyphicon-chevron-up').removeClass('glyphicon-chevron-down');
      });
    
    $('#serviceList').on('hidden.bs.collapse', function() {
        $(".servicedrop").addClass('glyphicon-chevron-down').removeClass('glyphicon-chevron-up');
      });
    
    0 讨论(0)
  • 2020-12-08 03:15

    Pure CSS.

    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>
            <i class="fa fa-chevron-down pull-right"></i>
        </a>
    

    The key element here is aria-expanded="false" or "true"

    CSS:

    a[aria-expanded=true] .fa-chevron-right {
       display: none;
    }
    a[aria-expanded=false] .fa-chevron-down {
       display: none;
    }
    
    0 讨论(0)
  • 2020-12-08 03:21

    The simplest answer I could find and thought it would be useful to add here for others.

    Essentially this involved this bit of css

    /* Rotating glyphicon when expanding/collapsing */
    .collapse-chevron .glyphicon {
      transition: .3s transform ease-in-out;
    }
    .collapse-chevron .collapsed .glyphicon {
      transform: rotate(-90deg);
    }
    

    which applied to this bit of html

    <div class="collapse-chevron">
      <a data-toggle="collapse" class="collapsed" href="#collapseFilter">
         <span class="glyphicon glyphicon-chevron-down" aria-hidden="true"></span>
         <strong>link to toggle</strong>
      </a>
      <div class="collapse" id="collapseFilter">
        <p>Some content I want collapsed or expanded</p>
      </div>
    </div>
    

    Codepen of this code: https://codepen.io/anon/pen/PKxzVa

    Source: this article

    See the codepen from the article for some more examples: https://codepen.io/disjfa/pen/EZdMpe

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

    fixed the issue changing HTML/CSS

    HTML:

    <a data-toggle="collapse" href="#doc" class="yt-toggle collapsed">View Doc 
        <i class="fa fa-caret-right fa-fw"></i> 
        <i class="fa fa-caret-down fa-fw"></i>
    </a>
    

    CSS:

    .yt-toggle.collapsed .fa-caret-down {
      display: none;
    }
    
    .yt-toggle.collapsed .fa-caret-right {
      display: inline-block;
    }
    
    .yt-toggle .fa-caret-right {
      display: none;
    }
    
    0 讨论(0)
  • 2020-12-08 03:26

    if you use angularjs you can try this.

    .html

    <button ng-click="enlarge(x.ID)" class="{{fullglyphon[x.ID]}}" ng-init="fullglyphon[x.ID] = 'btn btn-xs btn-primary glyphicon glyphicon-resize-full'"></button>
    

    .js

        $scope.enlarge = function(myID) { 
                    $scope.fullglyphon[myID] = "btn btn-xs btn-primary glyphicon glyphicon-resize-small";
    }
    

    toggle last one or do if comparison

    if ( $scope.fullglyphon[myID] == "btn btn-xs btn-primary glyphicon glyphicon-resize-small" ) {
                        $scope.fullglyphon[myID] = "btn btn-xs btn-primary glyphicon glyphicon-resize-full";
                }else{
                     $scope.fullglyphon[myID] = "btn btn-xs btn-primary glyphicon glyphicon-resize-small";
                    }
    
    0 讨论(0)
提交回复
热议问题