【问题标题】:Bootstrap 3 collapse change chevron icon on clickBootstrap 3 在点击时折叠更改 V 形图标
【发布时间】:2013-10-02 04:25:20
【问题描述】:

我已阅读有关我的问题的所有相关问题,并尝试了所有这些都无济于事。我似乎无法使我的代码工作,即使我“认为”我编写的几乎每个代码都与该站点上发布的解决方案相同。

这是 HTML 代码:

<div class="press-title">
  <p class="text" data-toggle="collapse" data-target="#serviceList">
    <span id="servicesButton" data-toggle="tooltip " data-original-title="Click Me!">
      <span class="servicedrop glyphicon glyphicon-chevron-down"></span> Services Offered <span class="servicedrop glyphicon glyphicon-chevron-down"></span>
    </span>
  </p>
</div>
<div id="serviceList" class="collapse">
  <div class="row featurette">
  ...

这是 JQuery

$('#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');
  }

我只想在折叠元素时将图标从向下更改为向上。然后在单击同一类时切换回来。 我真的被这个困住了。提前谢谢!

【问题讨论】:

标签: jquery twitter-bootstrap twitter-bootstrap-3


【解决方案1】:

纯 CSS。

HTML 部分:

   <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>

这里的关键元素是 aria-expanded="false" 或 "true"

CSS:

a[aria-expanded=true] .fa-chevron-right {
   display: none;
}
a[aria-expanded=false] .fa-chevron-down {
   display: none;
}

【讨论】:

  • 这是最好的解决方案,恕我直言。巧妙。
  • 最佳解决方案!好主意。
  • 我其实更喜欢这个解决方案。
  • 迄今为止最好的解决方案,在 FF、GCh、IE9+、Opera、safari、Android Explorer 和 Gch mobile 以及 iOS 的 Safari 上进行了测试,效果很好!
  • 简单、干净、易读总是最好的……这也是我的首选解决方案!
【解决方案2】:

问题在于您的 jQuery 代码不正确。

您将在这一行的早期关闭事件处理函数:

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

看到第二个右括号了吗?那就是提前关闭'on'功能。尝试将您的 jQuery 更改为如下所示:

$('#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');
  });

【讨论】:

  • 成功了!非常感谢!我还不太习惯 JQuery,因此出现了业余错误。感谢您的时间! :)
  • 在此处查看纯 CSS 解决方案:stackoverflow.com/a/33496943/5551593
【解决方案3】:

试试这个更优雅的解决方案:

$('#serviceList').click(function(){
    $(this).find('.servicedrop').toggleClass('icon-chevron-down icon-chevron-up');
});

【讨论】:

    【解决方案4】:

    与 Bojan 的回答类似,我使用此解决方案。
    像这样更改 HTML 代码:

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

    相对于.click,最好使用.on 事件。此外,通过使用类选择器,它可以用作站点范围的解决方案。

    $('.chevron_toggleable').on('click', function() {
        $(this).toggleClass('glyphicon-chevron-down glyphicon-chevron-up');
    });
    

    【讨论】:

    • 你好,它有效,但我看到的不是图标?图标你知道为什么
    • 我希望用户能够单击整个面板标题以展开/折叠,所以我使用了您的示例,但将单击事件放在包含面板标题的 div 上。 +1 - 感谢一个很好的例子。
    • 我喜欢这个例子,但由于某种原因它错过了点击(所以它会展开但不会改变图标)。没有错误,我不仅仅是导致问题的原因。
    【解决方案5】:

    纯 CSS,代码和动画更少。

    HTML 部分:

       <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);
    }
    

    【讨论】:

    • 不错!感谢分享。我的调整:从aria-expanded=truechevron-down 开始,然后是rotate(180deg),所以我把它指向下方,没有内容。
    • 不错且易于实现!
    【解决方案6】:

    我想提供与此处发布的另一个解决方案相同的选项,但使用带有变换的单个 div。这也有助于干净利落地使用过渡来为图标设置动画。

    `a[aria-expanded=true] .fa-chevron-right { 变换:旋转(0度); }

    a[aria-expanded=false] .fa-chevron-right { 变换:旋转(90度); // 或者你需要的任何方向 }`

    【讨论】:

    • 这是一个很好的解决方案,不涉及任何额外的 JS 代码。
    【解决方案7】:

    你可以试试这个。

    这是 HTML 代码:

    <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>
    

    这是 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" );
    });
    

    【讨论】:

    • 解释一下就好了!
    【解决方案8】:

    我能找到的最简单的答案,并认为在此处添加对其他人有用。

    基本上这涉及到这一点 css

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

    适用于这段 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:https://codepen.io/anon/pen/PKxzVa

    来源:this article

    有关更多示例,请参阅文章中的 codepen:https://codepen.io/disjfa/pen/EZdMpe

    【讨论】:

    • 不错,但与 BS 3 有问题(它不会在页面加载时呈现 chevron-down)(已通过 codepen 编辑确认)。
    • @cowbert 您需要确保将正确的类添加到起始 html。在我的示例中,它开始折叠,因此您需要像我一样拥有class="collapsed" ^^
    • 让它工作,谢谢!我之前使用的是 css aria-expanded 属性 match hide chevron 技巧。
    • 太棒了——这个甚至在状态之间有一个额外的小动画;)
    【解决方案9】:

    修复了更改 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;
    }
    

    【讨论】:

      【解决方案10】:
      <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>
      

      【讨论】:

        【解决方案11】:

        如果你使用 angularjs,你可以试试这个。

        .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";
        }
        

        切换最后一个或进行比较

        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";
                        }
        

        【讨论】:

          【解决方案12】:

          对于 Bootstrap 4 中的更改折叠图标,只需对 html 进行最少的更改,我已经完成了

          • 添加到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');
            }
            
          • 将字体放在合适的位置,与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
            
          • 并将class="collapsed" 添加到所有折叠(默认)链接:

            <a href="#info" data-toggle="collapse" class="collapsed">Collapsed link</a>
            <div id="info" class="collapse">
            

          【讨论】:

            猜你喜欢
            • 2017-09-11
            • 1970-01-01
            • 2013-08-11
            • 2023-03-28
            • 2017-09-10
            • 2013-10-28
            • 2014-02-07
            • 2017-08-30
            • 2019-01-09
            相关资源
            最近更新 更多