メインコンテンツへスキップ ドキュメントナビゲーションへスキップ

オフキャンバス

数個のクラスとJavaScriptプラグインを使って、ナビゲーションやショッピングカートなどのための隠されたサイドバーをプロジェクトに組み込みましょう。

仕組み

オフキャンバスは、JavaScriptを介してビューポートの左、右、上、または下端から表示するように切り替えることができるサイドバーコンポーネントです。ボタンまたはアンカーは、切り替える特定の要素に取り付けられたトリガーとして使用され、data属性はJavaScriptを呼び出すために使用されます。

  • オフキャンバスは、モーダルと同じJavaScriptコードを共有しています。概念的には非常に似ていますが、別のプラグインです。
  • 同様に、オフキャンバスのスタイルと寸法の一部のソースSass変数は、モーダルの変数から継承されます。
  • 表示されると、オフキャンバスには、クリックするとオフキャンバスを非表示にできるデフォルトの背景が含まれます。
  • モーダルと同様に、一度に表示できるオフキャンバスは1つだけです。

注意! CSSがアニメーションを処理する方法を考えると、.offcanvas要素でmarginまたはtranslateを使用することはできません。代わりに、クラスを独立したラッピング要素として使用してください。

このコンポーネントのアニメーション効果は、prefers-reduced-motionメディアクエリに依存します。アクセシビリティドキュメントのモーション軽減セクションを参照してください。

オフキャンバスコンポーネント

以下は、デフォルトで表示されるオフキャンバスの例です(.offcanvas.showを使用)。オフキャンバスには、閉じるボタン付きのヘッダーと、初期のpadding用のオプションのボディクラスのサポートが含まれています。可能な限り、非表示アクションを含むオフキャンバスヘッダーを含めるか、明示的な非表示アクションを提供することをお勧めします。

オフキャンバス
オフキャンバスのコンテンツはここに入ります。ほぼすべてのBootstrapコンポーネントまたはカスタム要素をここに配置できます。
html
<div class="offcanvas offcanvas-start show" tabindex="-1" id="offcanvas" aria-labelledby="offcanvasLabel">
  <div class="offcanvas-header">
    <h5 class="offcanvas-title" id="offcanvasLabel">Offcanvas</h5>
    <button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
  </div>
  <div class="offcanvas-body">
    Content for the offcanvas goes here. You can place just about any Bootstrap component or custom elements here.
  </div>
</div>

ライブデモ

以下のボタンを使用して、.offcanvasクラスを持つ要素で.showクラスを切り替えるJavaScriptを介してオフキャンバス要素を表示および非表示にします。

  • .offcanvasはコンテンツを非表示にします(デフォルト)
  • .offcanvas.showはコンテンツを表示します

href属性を持つリンク、またはdata-bs-target属性を持つボタンを使用できます。どちらの場合も、data-bs-toggle="offcanvas"が必要です。

href付きリンク
オフキャンバス
プレースホルダーとしてのテキスト。実際には、選択した要素を使用できます。テキスト、画像、リストなど。
html
<a class="btn btn-primary" data-bs-toggle="offcanvas" href="#offcanvasExample" role="button" aria-controls="offcanvasExample">
  Link with href
</a>
<button class="btn btn-primary" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasExample" aria-controls="offcanvasExample">
  Button with data-bs-target
</button>

<div class="offcanvas offcanvas-start" tabindex="-1" id="offcanvasExample" aria-labelledby="offcanvasExampleLabel">
  <div class="offcanvas-header">
    <h5 class="offcanvas-title" id="offcanvasExampleLabel">Offcanvas</h5>
    <button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
  </div>
  <div class="offcanvas-body">
    <div>
      Some text as placeholder. In real life you can have the elements you have chosen. Like, text, images, lists, etc.
    </div>
    <div class="dropdown mt-3">
      <button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown">
        Dropdown button
      </button>
      <ul class="dropdown-menu">
        <li><a class="dropdown-item" href="#">Action</a></li>
        <li><a class="dropdown-item" href="#">Another action</a></li>
        <li><a class="dropdown-item" href="#">Something else here</a></li>
      </ul>
    </div>
  </div>
</div>

ボディスクロール

オフキャンバスとその背景が表示されている場合、<body>要素のスクロールは無効になります。data-bs-scroll属性を使用して、<body>スクロールを有効にします。

ボディスクロール付きのオフキャンバス

このオプションの動作を確認するには、ページの残りの部分をスクロールしてみてください。

html
<button class="btn btn-primary" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasScrolling" aria-controls="offcanvasScrolling">Enable body scrolling</button>

<div class="offcanvas offcanvas-start" data-bs-scroll="true" data-bs-backdrop="false" tabindex="-1" id="offcanvasScrolling" aria-labelledby="offcanvasScrollingLabel">
  <div class="offcanvas-header">
    <h5 class="offcanvas-title" id="offcanvasScrollingLabel">Offcanvas with body scrolling</h5>
    <button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
  </div>
  <div class="offcanvas-body">
    <p>Try scrolling the rest of the page to see this option in action.</p>
  </div>
</div>

ボディスクロールと背景

表示される背景で<body>スクロールを有効にすることもできます。

スクロール付きの背景

このオプションの動作を確認するには、ページの残りの部分をスクロールしてみてください。

html
<button class="btn btn-primary" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasWithBothOptions" aria-controls="offcanvasWithBothOptions">Enable both scrolling & backdrop</button>

<div class="offcanvas offcanvas-start" data-bs-scroll="true" tabindex="-1" id="offcanvasWithBothOptions" aria-labelledby="offcanvasWithBothOptionsLabel">
  <div class="offcanvas-header">
    <h5 class="offcanvas-title" id="offcanvasWithBothOptionsLabel">Backdrop with scrolling</h5>
    <button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
  </div>
  <div class="offcanvas-body">
    <p>Try scrolling the rest of the page to see this option in action.</p>
  </div>
</div>

静的な背景

背景が静的に設定されている場合、オフキャンバスの外側をクリックしても閉じません。

オフキャンバス
外側をクリックしても閉じません。
html
<button class="btn btn-primary" type="button" data-bs-toggle="offcanvas" data-bs-target="#staticBackdrop" aria-controls="staticBackdrop">
  Toggle static offcanvas
</button>

<div class="offcanvas offcanvas-start" data-bs-backdrop="static" tabindex="-1" id="staticBackdrop" aria-labelledby="staticBackdropLabel">
  <div class="offcanvas-header">
    <h5 class="offcanvas-title" id="staticBackdropLabel">Offcanvas</h5>
    <button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
  </div>
  <div class="offcanvas-body">
    <div>
      I will not close if you click outside of me.
    </div>
  </div>
</div>

ダークオフキャンバス

v5.3.0で非推奨 v5.2.0で追加

ユーティリティを使用してオフキャンバスの外観を変更し、ダークナビバーなどのさまざまなコンテキストに合わせてより適切に一致させます。ここでは、.offcanvas.text-bg-darkを追加し、ダークオフキャンバスで適切なスタイルにするために.btn-close.btn-close-whiteを追加します。内部にドロップダウンがある場合は、.dropdown-menu.dropdown-menu-darkを追加することも検討してください。

注意! コンポーネントのダークバリアントは、カラーモードの導入に伴いv5.3.0で非推奨になりました。上記で言及したクラスを手動で追加する代わりに、ルート要素、親ラッパー、またはコンポーネント自体にdata-bs-theme="dark"を設定します。
オフキャンバス

オフキャンバスコンテンツをここに配置します。

html
<div class="offcanvas offcanvas-start show text-bg-dark" tabindex="-1" id="offcanvasDark" aria-labelledby="offcanvasDarkLabel">
  <div class="offcanvas-header">
    <h5 class="offcanvas-title" id="offcanvasDarkLabel">Offcanvas</h5>
    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="offcanvasDark" aria-label="Close"></button>
  </div>
  <div class="offcanvas-body">
    <p>Place offcanvas content here.</p>
  </div>
</div>

レスポンシブ

v5.2.0で追加

レスポンシブオフキャンバスクラスは、指定されたブレークポイント以下でビューポートの外にあるコンテンツを非表示にします。そのブレークポイントを超えると、内部のコンテンツは通常どおりに動作します。たとえば、.offcanvas-lglgブレークポイント以下のオフキャンバス内のコンテンツを非表示にしますが、lgブレークポイントより上のコンテンツを表示します。

ブラウザのサイズを変更して、レスポンシブオフキャンバスの切り替えを表示します。
レスポンシブオフキャンバス

これは、.offcanvas-lg内のコンテンツです。

html
<button class="btn btn-primary d-lg-none" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasResponsive" aria-controls="offcanvasResponsive">Toggle offcanvas</button>

<div class="alert alert-info d-none d-lg-block">Resize your browser to show the responsive offcanvas toggle.</div>

<div class="offcanvas-lg offcanvas-end" tabindex="-1" id="offcanvasResponsive" aria-labelledby="offcanvasResponsiveLabel">
  <div class="offcanvas-header">
    <h5 class="offcanvas-title" id="offcanvasResponsiveLabel">Responsive offcanvas</h5>
    <button type="button" class="btn-close" data-bs-dismiss="offcanvas" data-bs-target="#offcanvasResponsive" aria-label="Close"></button>
  </div>
  <div class="offcanvas-body">
    <p class="mb-0">This is content within an <code>.offcanvas-lg</code>.</p>
  </div>
</div>

レスポンシブオフキャンバスクラスは、各ブレークポイントで使用できます。

  • .offcanvas
  • .offcanvas-sm
  • .offcanvas-md
  • .offcanvas-lg
  • .offcanvas-xl
  • .offcanvas-xxl

配置

オフキャンバスコンポーネントのデフォルトの配置はないため、以下の修飾クラスのいずれかを追加する必要があります。

  • .offcanvas-startは、ビューポートの左側にオフキャンバスを配置します(上記に表示)
  • .offcanvas-endは、ビューポートの右側にオフキャンバスを配置します
  • .offcanvas-topは、ビューポートの上部にオフキャンバスを配置します
  • .offcanvas-bottomは、ビューポートの下部にオフキャンバスを配置します

以下の上、右、下の例を試してみてください。

オフキャンバス上部
...
html
<button class="btn btn-primary" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasTop" aria-controls="offcanvasTop">Toggle top offcanvas</button>

<div class="offcanvas offcanvas-top" tabindex="-1" id="offcanvasTop" aria-labelledby="offcanvasTopLabel">
  <div class="offcanvas-header">
    <h5 class="offcanvas-title" id="offcanvasTopLabel">Offcanvas top</h5>
    <button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
  </div>
  <div class="offcanvas-body">
    ...
  </div>
</div>
オフキャンバス右
...
html
<button class="btn btn-primary" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasRight" aria-controls="offcanvasRight">Toggle right offcanvas</button>

<div class="offcanvas offcanvas-end" tabindex="-1" id="offcanvasRight" aria-labelledby="offcanvasRightLabel">
  <div class="offcanvas-header">
    <h5 class="offcanvas-title" id="offcanvasRightLabel">Offcanvas right</h5>
    <button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
  </div>
  <div class="offcanvas-body">
    ...
  </div>
</div>
オフキャンバス下部
...
html
<button class="btn btn-primary" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasBottom" aria-controls="offcanvasBottom">Toggle bottom offcanvas</button>

<div class="offcanvas offcanvas-bottom" tabindex="-1" id="offcanvasBottom" aria-labelledby="offcanvasBottomLabel">
  <div class="offcanvas-header">
    <h5 class="offcanvas-title" id="offcanvasBottomLabel">Offcanvas bottom</h5>
    <button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
  </div>
  <div class="offcanvas-body small">
    ...
  </div>
</div>

アクセシビリティ

オフキャンバスパネルは概念的にモーダルダイアログであるため、.offcanvasaria-labelledby="..."(オフキャンバスのタイトルを参照)を追加してください。JavaScriptを介してrole="dialog"を既に追加しているため、追加する必要はないことに注意してください。

CSS

変数

v5.2.0で追加

Bootstrapの進化するCSS変数アプローチの一部として、オフキャンバスは、リアルタイムのカスタマイズを強化するために、.offcanvasでローカルCSS変数を使用するようになりました。CSS変数の値はSassを介して設定されるため、Sassのカスタマイズも引き続きサポートされています。

--#{$prefix}offcanvas-zindex: #{$zindex-offcanvas};
--#{$prefix}offcanvas-width: #{$offcanvas-horizontal-width};
--#{$prefix}offcanvas-height: #{$offcanvas-vertical-height};
--#{$prefix}offcanvas-padding-x: #{$offcanvas-padding-x};
--#{$prefix}offcanvas-padding-y: #{$offcanvas-padding-y};
--#{$prefix}offcanvas-color: #{$offcanvas-color};
--#{$prefix}offcanvas-bg: #{$offcanvas-bg-color};
--#{$prefix}offcanvas-border-width: #{$offcanvas-border-width};
--#{$prefix}offcanvas-border-color: #{$offcanvas-border-color};
--#{$prefix}offcanvas-box-shadow: #{$offcanvas-box-shadow};
--#{$prefix}offcanvas-transition: #{transform $offcanvas-transition-duration ease-in-out};
--#{$prefix}offcanvas-title-line-height: #{$offcanvas-title-line-height};

Sass変数

$offcanvas-padding-y:               $modal-inner-padding;
$offcanvas-padding-x:               $modal-inner-padding;
$offcanvas-horizontal-width:        400px;
$offcanvas-vertical-height:         30vh;
$offcanvas-transition-duration:     .3s;
$offcanvas-border-color:            $modal-content-border-color;
$offcanvas-border-width:            $modal-content-border-width;
$offcanvas-title-line-height:       $modal-title-line-height;
$offcanvas-bg-color:                var(--#{$prefix}body-bg);
$offcanvas-color:                   var(--#{$prefix}body-color);
$offcanvas-box-shadow:              $modal-content-box-shadow-xs;
$offcanvas-backdrop-bg:             $modal-backdrop-bg;
$offcanvas-backdrop-opacity:        $modal-backdrop-opacity;

使い方

オフキャンバスプラグインは、いくつかのクラスと属性を利用して、重い処理を処理します

  • .offcanvasはコンテンツを非表示にします
  • .offcanvas.showはコンテンツを表示します
  • .offcanvas-startは、左側のオフキャンバスを非表示にします
  • .offcanvas-endは、右側のオフキャンバスを非表示にします
  • .offcanvas-topは、上部のオフキャンバスを非表示にします
  • .offcanvas-bottom は、オフキャンバスを下部に隠します。

JavaScriptの機能をトリガーするdata-bs-dismiss="offcanvas"属性を持つ閉じるボタンを追加します。すべてのデバイスで正しく動作させるために、必ず<button>要素を使用してください。

データ属性を使用する

切り替え

data-bs-toggle="offcanvas"data-bs-targetまたはhrefを要素に追加すると、オフキャンバス要素の制御を自動的に割り当てます。data-bs-target属性は、オフキャンバスを適用するCSSセレクターを受け入れます。オフキャンバス要素には必ずoffcanvasクラスを追加してください。デフォルトで開いた状態にしたい場合は、追加でshowクラスを追加してください。

閉じる

閉じる機能は、以下に示すように、オフキャンバス内のボタンにdata-bs-dismiss属性を使用することで実現できます。

<button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>

または、以下に示すように、追加のdata-bs-targetを使用して、オフキャンバス外のボタンで実現できます。

<button type="button" class="btn-close" data-bs-dismiss="offcanvas" data-bs-target="#my-offcanvas" aria-label="Close"></button>
オフキャンバスを閉じる方法はどちらもサポートされていますが、オフキャンバス外から閉じる方法はARIA Authoring Practices Guideのダイアログ(モーダル)パターンには一致しないことに注意してください。自己責任で行ってください。

JavaScriptを使用する

以下を使用して手動で有効化します。

const offcanvasElementList = document.querySelectorAll('.offcanvas')
const offcanvasList = [...offcanvasElementList].map(offcanvasEl => new bootstrap.Offcanvas(offcanvasEl))

オプション

オプションはデータ属性またはJavaScript経由で渡せるため、data-bs-にオプション名を追加できます(例:data-bs-animation="{value}")。データ属性経由でオプションを渡す場合は、オプション名のケースタイプを "camelCase" から "kebab-case" に変更してください。たとえば、data-bs-customClass="beautifier" の代わりに data-bs-custom-class="beautifier" を使用します。

Bootstrap 5.2.0 以降、すべてのコンポーネントは、JSON 文字列として単純なコンポーネント構成を格納できる実験的な予約済みデータ属性 data-bs-config をサポートしています。要素に data-bs-config='{"delay":0, "title":123}' および data-bs-title="456" 属性がある場合、最終的な title の値は 456 になり、個別のデータ属性は data-bs-config で指定された値をオーバーライドします。さらに、既存のデータ属性は data-bs-delay='{"show":0,"hide":150}' のように JSON 値を格納できます。

最終的な構成オブジェクトは、data-bs-configdata-bs-、および js object がマージされた結果であり、最後に指定されたキーと値が他の値をオーバーライドします。

名前 デフォルト 説明
backdrop boolean または 文字列 static true オフキャンバスが開いている間、ボディに背景を適用します。または、クリック時にオフキャンバスを閉じない背景の場合はstaticを指定します。
keyboard boolean true Escapeキーが押されるとオフキャンバスを閉じます。
scroll boolean false オフキャンバスが開いている間、ボディのスクロールを許可します。

メソッド

すべてのAPIメソッドは非同期であり、トランジションを開始します。トランジションが開始されるとすぐに呼び出し元に戻りますが、完了する前です。さらに、トランジション中のコンポーネントでのメソッド呼び出しは無視されます。JavaScriptドキュメントの詳細をご覧ください。

コンテンツをオフキャンバス要素としてアクティブにします。オプションの object を受け入れます。

たとえば、コンストラクターを使用してオフキャンバスインスタンスを作成できます。

const bsOffcanvas = new bootstrap.Offcanvas('#myOffcanvas')
メソッド 説明
dispose 要素のオフキャンバスを破棄します。
getInstance 静的メソッドで、DOM要素に関連付けられたオフキャンバスインスタンスを取得できます。
getOrCreateInstance 静的メソッドで、DOM要素に関連付けられたオフキャンバスインスタンスを取得できます。初期化されていない場合は、新しいインスタンスを作成します。
hide オフキャンバス要素を非表示にします。オフキャンバス要素が実際に非表示になる前(つまり、hidden.bs.offcanvasイベントが発生する前)に、呼び出し元に戻ります。
show オフキャンバス要素を表示します。オフキャンバス要素が実際に表示される前(つまり、shown.bs.offcanvasイベントが発生する前)に、呼び出し元に戻ります。
toggle オフキャンバス要素の表示/非表示を切り替えます。オフキャンバス要素が実際に表示または非表示になる前(つまり、shown.bs.offcanvasまたはhidden.bs.offcanvasイベントが発生する前)に、呼び出し元に戻ります。

イベント

Bootstrapのオフキャンバスクラスは、オフキャンバス機能にフックするためのいくつかのイベントを公開しています。

イベントタイプ 説明
hide.bs.offcanvas hideメソッドが呼び出されるとすぐに発生するイベント。
hidden.bs.offcanvas オフキャンバス要素がユーザーから非表示になったときに発生するイベント(CSSトランジションが完了するまで待機します)。
hidePrevented.bs.offcanvas オフキャンバスが表示され、その背景がstaticであり、オフキャンバスの外側をクリックした場合に発生するイベント。また、Escapeキーが押され、keyboardオプションがfalseに設定されている場合にも発生します。
show.bs.offcanvas showインスタンスメソッドが呼び出されるとすぐに発生するイベント。
shown.bs.offcanvas オフキャンバス要素がユーザーに表示されたときに発生するイベント(CSSトランジションが完了するまで待機します)。
const myOffcanvas = document.getElementById('myOffcanvas')
myOffcanvas.addEventListener('hidden.bs.offcanvas', event => {
  // do something...
})