折りたたみ(collapse)

collapse.js

クラスを少しだけ活用して切り替え動作を簡単にする柔軟なプラグインです。

プラグイン依存
折りたたみ(collapse)は、そのBootstrapのバージョンのトランジションのプラグインを必要とします。

使用例

ボタンをクリックすることで、クラスの変更を通じてその下の別の要素の表示/非表示を行います。

  • .collapseはコンテンツを非表示時にします。
  • .collapsingはトランジション期間(移り変わりの状態)を適用します。
  • .collapse.inはコンテンツを表示します。

href属性付きのリンク、またはdata-target属性付きのボタンを使用することが可能です。 いずれの場合も、data-toggle="collapse"が必須です。

<a class="btn btn-primary" role="button" data-toggle="collapse" href="#collapseExample" aria-expanded="false" aria-controls="collapseExample">
  Link with href
</a>
<button class="btn btn-primary" type="button" data-toggle="collapse" data-target="#collapseExample" aria-expanded="false" aria-controls="collapseExample">
  Button with data-target
</button>
<div class="collapse" id="collapseExample">
  <div class="well">
    ...
  </div>
</div>

アコーディオンの例

デフォルトの折りたたみ(collapse)の挙動を拡張し、パネルコンポーネントを使用してアコーディオンを作成します。

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.
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.
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 class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
  <div class="panel panel-default">
    <div class="panel-heading" role="tab" id="headingOne">
      <h4 class="panel-title">
        <a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
          Collapsible Group Item #1
        </a>
      </h4>
    </div>
    <div id="collapseOne" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne">
      <div class="panel-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="panel panel-default">
    <div class="panel-heading" role="tab" id="headingTwo">
      <h4 class="panel-title">
        <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
          Collapsible Group Item #2
        </a>
      </h4>
    </div>
    <div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
      <div class="panel-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="panel panel-default">
    <div class="panel-heading" role="tab" id="headingThree">
      <h4 class="panel-title">
        <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
          Collapsible Group Item #3
        </a>
      </h4>
    </div>
    <div id="collapseThree" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingThree">
      <div class="panel-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>

.panel-body.list-groupに取り替えることも可能です。

展開/折りたたみの制御のアクセシビリティについて

制御用の要素にaria-expandedを追加するようにして下さい。 この属性は、スクリーンリーダーと同様の補助技術に対して折りたたみ可能(collapsible)な要素の現在の状態を明確に定義します。 もし、折りたたみ可能な要素がデフォルトで閉じられている場合、aria-expanded="false"の値を持つべきです。 もし、折りたたみ可能な要素をクラスを使用することでデフォルトで開くように設定するには、 代わりに制御用の要素にaria-expanded="true"を設定します。 このプラグインは折りたたみ要素が開いているか閉じているかで、自動的にこの属性を切り替えます。

加えて、もし制御用の要素が単一の折りたたみ可能な要素を対象としている場合 (すなわち、data-target属性がidセレクタを示している場合)、 制御用の要素に折りたたみ要素のidを含むaria-controlsを追加することが可能です。 モダンなスクリーンリーダーと同様の補助技術は、 この属性を使用して折りたたみ可能な要素自身を直接ナビゲートする追加のショートカットをユーザーに提供してくれます。

使用方法

collapseプラグインは、その動作のために幾つかのクラスを活用します。

  • .collapseはコンテンツを隠します。
  • .collapse.inはコンテンツを表示します。
  • .collapsingはトランジションの開始時に追加され、完了時に削除されます。

これらのクラスは、component-animations.less内で見つけることが出来ます。

data属性を使用

要素へdata-toggle="collapse"data-targetを追加するだけで、 折りたたみ要素への制御が自動的に割り当てられます。 data-target属性は折りたたみを適用するためにCSSセレクタを受け取ります。 collapseクラスを折りたたみ要素へ、追加するようにしてください。 もしデフォルトで折りたたみを開いておきたいのであれば、inクラスを追加してください。

JavaScriptを使用

手動で有効化します。

$('.collapse').collapse()

オプション

オプションはdata属性またはJavaScriptを通して渡すことが可能です。 data属性では、data-parent=""のようにオプション名の前にdata-を追加します。

名前 デフォルト 説明
parent selector false このセレクタが提供された場合、指定されたparent配下の全ての折りたたみ要素は、 この折りたたみの項目が表示された際に閉じられます。 (伝統的なアコーディオンの挙動に似ていて、これpanelクラスに依存します。)
toggle boolean true 折りたたみ要素を切り替えます。

オプション

.collapse(options)

コンテンツを切り替え要素として有効化します。 任意のoptionsオブジェクトを受け取ります。

$('#myCollapsible').collapse({
  toggle: false
})

.collapse('toggle')

折りたたみ要素の表示/非表示を切り替えます。

.collapse('show')

折りたたみ要素を表示します。

.collapse('hide')

折りたたみ要素を非表示にします。

メソッド

Bootstrapのcollapseクラスには、折りたたみ機能へのフックのために幾つかのイベントが用意されています。

イベントの型 説明
show.bs.collapse このイベントはshowインスタンスメソッドが呼び出された際に即座に発火されます。
shown.bs.collapse このイベントは折りたたみ要素がユーザーに対して表示された際に発火されます。 (CSSトランジションの完了を待ちます。)
hide.bs.collapse このイベントはhideメソッドが呼び出された際に即座に発火されます。
hidden.bs.collapse このイベントは折りたたみ要素がユーザーに対して非表示にされた際に発火されます。 (CSSトランジションの完了を待ちます。)
$('#myCollapsible').on('hidden.bs.collapse', function () {
  // do something…
})

 Back to top

docs CC BY 3.0

このページは、ページトップのリンク先のBootstrap公式ドキュメント内のページを翻訳した内容を基に構成されています。 下記の項目を確認し、必要に応じて公式のドキュメントをご確認ください。 もし、誤訳などの間違いを見つけましたら、 @tomofまで教えていただければ幸いです。

  • 元のコンテンツと比べてドキュメントの情報が古くなっている可能性があります。
  • "訳注:"などの断わりを入れた上で、日本人向けの情報やより分かり易くするための追記を行っている事があります。