モーダル
Bootstrap の JavaScript モーダル プラグインを使用して、サイトにダイアログを追加して、ライトボックス、ユーザー通知、または完全にカスタム コンテンツを表示します。
使い方
メニュー オプションが最近変更されたため、Bootstrap のモーダル コンポーネントを使い始める前に、必ず以下をお読みください。
- モーダルは、HTML、CSS、および JavaScript で構築されます。ドキュメント内の他のすべての上に配置され、スクロールを削除して<body>、モーダル コンテンツが代わりにスクロールするようにします。
- モーダルの「背景」をクリックすると、モーダルが自動的に閉じます。
- Bootstrap は、一度に 1 つのモーダル ウィンドウのみをサポートします。ネストされたモーダルは、ユーザー エクスペリエンスが低下すると考えられるため、サポートされていません。
- モーダルは を使用しますposition: fixed。これは、レンダリングに関して少し特殊な場合があります。可能な限り、モーダル HTML を最上位の位置に配置して、他の要素からの潜在的な干渉を回避します。.modal別の固定要素内に a をネストすると、問題が発生する可能性があります。
- もう一度position: fixed言いますが、モバイル デバイスでモーダルを使用する際にはいくつかの注意事項があります。詳細については、ブラウザのサポート ドキュメントを参照してください。
- HTML5 がセマンティクスを定義する方法により、autofocusHTML 属性は Bootstrap モーダルでは効果がありません。同じ効果を得るには、カスタム JavaScript を使用します。
$('#myModal').on('shown.bs.modal', function () {
  $('#myInput').trigger('focus')
})このコンポーネントのアニメーション効果は、prefers-reduced-motionメディア クエリに依存します。アクセシビリティ ドキュメントの縮小モーション セクションを参照してください。
デモと使用ガイドラインについては、引き続きお読みください。
例
モーダル コンポーネント
以下は、静的モーダルの例です (つまり、positionとdisplayがオーバーライドされています)。モーダル ヘッダー、モーダル ボディ ( に必須padding)、およびモーダル フッター (オプション) が含まれます。可能な限り却下アクションを含むモーダル ヘッダーを含めるか、別の明示的な却下アクションを提供するようお願いします。
<div class="modal" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">×</span>
        </button>
      </div>
      <div class="modal-body">
        <p>Modal body text goes here.</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>ライブデモ
下のボタンをクリックして、動作中のモーダル デモを切り替えます。ページの上部から下にスライドしてフェードインします。
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
  Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">×</span>
        </button>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>長いコンテンツのスクロール
ユーザーのビューポートまたはデバイスに対してモーダルが長くなりすぎると、ページ自体とは無関係にスクロールします。以下のデモを試して、私たちが意味することを確認してください。
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalLong">
  Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModalLong" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">×</span>
        </button>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>垂直方向に中央揃え
モーダルを垂直方向に中央揃えに追加.modal-dialog-centeredします。.modal-dialog
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalCenter">
  Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalCenterTitle">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">×</span>
        </button>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>ツールチップとポップオーバー
ツールチップとポップオーバーは、必要に応じてモーダル内に配置できます。モーダルが閉じられると、その中のツールチップとポップオーバーも自動的に閉じられます。
<div class="modal-body">
  <h5>Popover in a modal</h5>
  <p>This <a href="#" role="button" class="btn btn-secondary popover-test" title="Popover title" data-content="Popover body content is set in this attribute.">button</a> triggers a popover on click.</p>
  <hr>
  <h5>Tooltips in a modal</h5>
  <p><a href="#" class="tooltip-test" title="Tooltip">This link</a> and <a href="#" class="tooltip-test" title="Tooltip">that link</a> have tooltips on hover.</p>
</div>グリッドの使用
.container-fluid内でネストすることにより、モーダル内で Bootstrap グリッド システムを利用し.modal-bodyます。次に、他の場所と同じように、通常のグリッド システム クラスを使用します。
<div class="modal-body">
  <div class="container-fluid">
    <div class="row">
      <div class="col-md-4">.col-md-4</div>
      <div class="col-md-4 ml-auto">.col-md-4 .ml-auto</div>
    </div>
    <div class="row">
      <div class="col-md-3 ml-auto">.col-md-3 .ml-auto</div>
      <div class="col-md-2 ml-auto">.col-md-2 .ml-auto</div>
    </div>
    <div class="row">
      <div class="col-md-6 ml-auto">.col-md-6 .ml-auto</div>
    </div>
    <div class="row">
      <div class="col-sm-9">
        Level 1: .col-sm-9
        <div class="row">
          <div class="col-8 col-sm-6">
            Level 2: .col-8 .col-sm-6
          </div>
          <div class="col-4 col-sm-6">
            Level 2: .col-4 .col-sm-6
          </div>
        </div>
      </div>
    </div>
  </div>
</div>さまざまなモーダル コンテンツ
内容がわずかに異なる同じモーダルをすべてトリガーするボタンがたくさんありますか? event.relatedTargetおよびHTMLdata-*属性を(おそらくjQuery 経由で)使用して、クリックされたボタンに応じてモーダルの内容を変更します。
以下はライブ デモで、HTML と JavaScript の例が続きます。詳細については、モーダル イベントのドキュメントを参照してくださいrelatedTarget。
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@mdo">Open modal for @mdo</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@fat">Open modal for @fat</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@getbootstrap">Open modal for @getbootstrap</button>
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">New message</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">×</span>
        </button>
      </div>
      <div class="modal-body">
        <form>
          <div class="form-group">
            <label for="recipient-name" class="col-form-label">Recipient:</label>
            <input type="text" class="form-control" id="recipient-name">
          </div>
          <div class="form-group">
            <label for="message-text" class="col-form-label">Message:</label>
            <textarea class="form-control" id="message-text"></textarea>
          </div>
        </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Send message</button>
      </div>
    </div>
  </div>
</div>$('#exampleModal').on('show.bs.modal', function (event) {
  var button = $(event.relatedTarget) // Button that triggered the modal
  var recipient = button.data('whatever') // Extract info from data-* attributes
  // If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
  // Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
  var modal = $(this)
  modal.find('.modal-title').text('New message to ' + recipient)
  modal.find('.modal-body input').val(recipient)
})アニメーションを変更
変数はモーダル フェードイン アニメーションの前$modal-fade-transformの変換状態を決定し、変数はモーダル フェードイン アニメーションの終了時の変換を決定します。.modal-dialog$modal-show-transform.modal-dialog
たとえば、ズームイン アニメーションが必要な場合は、 を設定できます$modal-fade-transform: scale(.8)。
アニメーションを削除
フェードインして表示するのではなく、単に表示するモーダルの場合.fadeは、モーダル マークアップからクラスを削除します。
<div class="modal" tabindex="-1" role="dialog" aria-labelledby="..." aria-hidden="true">
  ...
</div>動的高さ
$('#myModal').modal('handleUpdate')開いているモーダルの高さが変更された場合、スクロールバーが表示された場合にモーダルの位置を再調整するために呼び出す必要があります。
アクセシビリティ
モーダル タイトルを参照して、とをrole="dialog"とに必ず追加してください。さらに、モーダル ダイアログの説明をon で指定できます。aria-labelledby="...".modalrole="document".modal-dialogaria-describedby.modal
YouTube 動画の埋め込み
モーダルに YouTube 動画を埋め込むには、再生を自動的に停止するなど、Bootstrap にはない追加の JavaScript が必要です。詳細については、このスタック オーバーフローの投稿を参照してください。
オプションサイズ
モーダルには 3 つのオプションのサイズがあり、修飾子クラスを介して に配置できます.modal-dialog。これらのサイズは、特定のブレークポイントで開始され、狭いビューポートでの水平スクロールバーを回避します。
| サイズ | クラス | モーダル最大幅 | 
|---|---|---|
| 小さな | .modal-sm | 300px | 
| デフォルト | なし | 500px | 
| 大きい | .modal-lg | 800px | 
| 特大 | .modal-xl | 1140px | 
修飾子クラスのないデフォルトのモーダルは、「中」サイズのモーダルを構成します。
<!-- Extra large modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bd-example-modal-xl">Extra large modal</button>
<div class="modal fade bd-example-modal-xl" tabindex="-1" role="dialog" aria-labelledby="myExtraLargeModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-xl">
    <div class="modal-content">
      ...
    </div>
  </div>
</div>
<!-- Large modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bd-example-modal-lg">Large modal</button>
<div class="modal fade bd-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
      ...
    </div>
  </div>
</div>
<!-- Small modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bd-example-modal-sm">Small modal</button>
<div class="modal fade bd-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-sm">
    <div class="modal-content">
      ...
    </div>
  </div>
</div>使用法
モーダル プラグインは、データ属性または JavaScript を介して、非表示のコンテンツをオンデマンドで切り替えます。また、デフォルトのスクロール動作をオーバーライドする に追加.modal-openし、モーダルの外側をクリックしたときに表示されたモーダルを閉じるためのクリック領域を提供する を生成します。<body>.modal-backdrop
データ属性経由
JavaScript を書かずにモーダルを有効にします。特定のモーダルを切り替え対象にするために、 ordata-toggle="modal"とともに、ボタンなどのコントローラー要素に設定します。data-target="#foo"href="#foo"
<button type="button" data-toggle="modal" data-target="#myModal">Launch modal</button>JavaScript 経由
myModalJavaScript の 1 行でid を使用してモーダルを呼び出します。
$('#myModal').modal(options)オプション
オプションは、データ属性または JavaScript を介して渡すことができます。data-データ属性の場合、オプション名をのようにに追加しますdata-backdrop=""。
| 名前 | タイプ | デフォルト | 説明 | 
|---|---|---|---|
| 背景 | ブール値または文字列 'static' | 真実 | モーダル背景要素が含まれています。 staticまたは、クリック時にモーダルを閉じない背景を指定します。 | 
| キーボード | ブール値 | 真実 | エスケープキーが押されたときにモーダルを閉じます | 
| 集中 | ブール値 | 真実 | 初期化時にモーダルにフォーカスを置きます。 | 
| 見せる | ブール値 | 真実 | 初期化時にモーダルを表示します。 | 
メソッド
非同期メソッドと遷移
すべての API メソッドは非同期であり、遷移を開始します。トランジションが開始されるとすぐに呼び出し元に戻りますが、トランジションが終了する前に戻ります。さらに、遷移中のコンポーネントでのメソッド呼び出しは無視されます。
.modal(options)
 
     コンテンツをモーダルとしてアクティブにします。オプションのオプションを受け入れますobject。
$('#myModal').modal({
  keyboard: false
}).modal('toggle')
 
     モーダルを手動で切り替えます。モーダルが実際に表示または非表示になる前 (つまり、orイベントが発生する前)に呼び出し元に戻ります。shown.bs.modalhidden.bs.modal
$('#myModal').modal('toggle').modal('show')
 
     モーダルを手動で開きます。モーダルが実際に表示される前 (つまり、イベントが発生する前)に呼び出し元に戻ります。shown.bs.modal
$('#myModal').modal('show').modal('hide')
 
     モーダルを手動で非表示にします。モーダルが実際に非表示になる前 (つまり、イベントが発生する前)に呼び出し元に戻ります。hidden.bs.modal
$('#myModal').modal('hide').modal('handleUpdate')
 
     モーダルが開いている間にモーダルの高さが変化した場合 (つまり、スクロールバーが表示された場合) は、モーダルの位置を手動で再調整します。
$('#myModal').modal('handleUpdate').modal('dispose')
 
     要素のモーダルを破棄します。
イベント
Bootstrap のモーダル クラスは、モーダル機能にフックするためのいくつかのイベントを公開します。すべてのモーダル イベントは、モーダル自体 (つまり<div class="modal">) で発生します。
| イベントタイプ | 説明 | 
|---|---|
| show.bs.modal | showこのイベントは、インスタンス メソッドが呼び出されるとすぐに発生します。クリックが原因の場合、クリックされた要素はrelatedTargetイベントのプロパティとして使用できます。 | 
| shown.bs.modal | このイベントは、モーダルがユーザーに表示されるようになったときに発生します (CSS トランジションが完了するのを待ちます)。クリックが原因の場合、クリックされた要素は relatedTargetイベントのプロパティとして使用できます。 | 
| hide.bs.modal | hideこのイベントは、インスタンス メソッドが呼び出されるとすぐに発生します。 | 
| hidden.bs.modal | このイベントは、モーダルがユーザーから非表示にされたときに発生します (CSS トランジションが完了するのを待ちます)。 | 
$('#myModal').on('hidden.bs.modal', function (e) {
  // do something...
})