Bootstrap3でアイコン付アコーディオン(Collapse)を使用する

注釈

開く show.bs.collapse イベントと閉じる hide.bs.collapse イベント発生時に、アイコン(class)を削除&追加する。

デモ

panel-group

list-group


ソース

panel-group

HTML

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
  <div class="panel-group" id="accordion">
    <div class="panel panel-default">
      <div class="panel-heading">
        <h4 class="panel-title">
          <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne">
            <span class="glyphicon glyphicon-flag text-success"></span>折りたたみグループアイテム #1
            <span class="glyphicon glyphicon-collapse-up pull-right"></span>
          </a>
        </h4>
      </div>
      <div id="collapseOne" class="panel-collapse collapse in">
        <div class="panel-body">1個目の内容</div>
      </div>
    </div>
    <div class="panel panel-default">
      <div class="panel-heading">
        <h4 class="panel-title">
          <a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">
            <span class="glyphicon glyphicon-flag text-info"></span>折りたたみグループアイテム #2
            <span class="glyphicon glyphicon-collapse-down pull-right"></span>
          </a>
        </h4>
      </div>
      <div id="collapseTwo" class="panel-collapse collapse">
        <div class="panel-body">2個目の内容</div>
      </div>
    </div>
    <div class="panel panel-default">
      <div class="panel-heading">
        <h4 class="panel-title">
          <a data-toggle="collapse" data-parent="#accordion" href="#collapseThree">
            <span class="glyphicon glyphicon-flag text-danger"></span>折りたたみグループアイテム #3
            <span class="glyphicon glyphicon-collapse-down pull-right"></span>
          </a>
        </h4>
      </div>
      <div id="collapseThree" class="panel-collapse collapse">
        <div class="panel-body">3個目の内容</div>
      </div>
    </div>
  </div>

Script

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
  <script>
  (function($) {
    'use strict';
    $('#collapseOne, #collapseTwo, #collapseThree').on({
      // 折り畳み開く処理
      'show.bs.collapse': function() {
        $('a[href="#' + this.id + '"] span.glyphicon-collapse-down')
          .removeClass('glyphicon-collapse-down')
          .addClass('glyphicon-collapse-up');
      },
      // 折り畳み閉じる処理
      'hide.bs.collapse': function() {
        $('a[href="#' + this.id + '"] span.glyphicon-collapse-up')
          .removeClass('glyphicon-collapse-up')
          .addClass('glyphicon-collapse-down');
      }
    });
  })(jQuery);
  </script>

list-group

HTML

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  <div id="menu">
    <div class="panel list-group">
      <a href="#" class="list-group-item">
        <span class="glyphicon glyphicon-home"></span> ホーム
        <span class="glyphicon glyphicon-chevron-right pull-right"></span>
      </a>
      <a href="#menuOne" class="list-group-item" data-toggle="collapse" data-target="#menuOne" data-parent="#menu">
        <span class="glyphicon glyphicon-envelope"></span> メッセージ <span class="label label-success">3</span>
        <span class="glyphicon glyphicon-chevron-down pull-right"></span>
      </a>
      <div id="menuOne" class="sublinks collapse">
        <a class="list-group-item small"> 受信ボックス<span class="glyphicon glyphicon-chevron-right pull-right"></span></a>
        <a class="list-group-item small"> 送信ボックス<span class="glyphicon glyphicon-chevron-right pull-right"></span></a>
      </div>
      <a href="#menuTwo" class="list-group-item" data-toggle="collapse" data-target="#menuTwo" data-parent="#menu">
        <span class="glyphicon glyphicon-cog"></span> ツール
        <span class="glyphicon glyphicon-chevron-up pull-right"></span>
      </a>
      <div id="menuTwo" class="sublinks collapse in">
        <a class="list-group-item small"> ツール1<span class="glyphicon glyphicon-chevron-right pull-right"></span></a>
      </div>
    </div>
  </div>

Script

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  <script>
  (function($) {
    'use strict';
    // Collapseイベント
    $('#menuOne, #menuTwo').on({
      // 折り畳み開く処理
      'show.bs.collapse': function() {
        $('a[href="#' + this.id + '"] span.glyphicon-chevron-down')
          .removeClass('glyphicon-chevron-down')
          .addClass('glyphicon-chevron-up');
      },
      // 折り畳み閉じる処理
      'hide.bs.collapse': function() {
        $('a[href="#' + this.id + '"] span.glyphicon-chevron-up')
          .removeClass('glyphicon-chevron-up')
          .addClass('glyphicon-chevron-down');
      }
    });
    // ハッシュリンクキャンセル
    $('a[href="#menuOne"], a[href="#menuTwo"]').on('click', function(event) {
      event.preventDefault();
    });
  })(jQuery);
  </script>