ホームページ  >  記事  >  ウェブフロントエンド  >  Bootstrapのアコーディオンコンポーネントの使い方を徹底解説(例)

Bootstrapのアコーディオンコンポーネントの使い方を徹底解説(例)

青灯夜游
青灯夜游転載
2021-11-26 19:51:053245ブラウズ

Bootstrap にアコーディオンを追加するにはどうすればよいですか?次の記事では、Bootstrap5 アコーディオン コンポーネントの使用方法をコード例を示して説明します。

Bootstrapのアコーディオンコンポーネントの使い方を徹底解説(例)

1 簡単な例

Accordion コンポーネントはタブと非常によく似ていますが、水平方向ではなく垂直方向に配置され、含まれている JavaScript と組み合わされる点が異なります。 jsを手書きすることなく、折り畳まれた縦書きコンテンツを実現できるプラグインです。アコーディオンは、バックエンド パネルの垂直ナビゲーション メニュー、フロントエンドの折りたたみメッセージなどでよく使用されます。 [関連する推奨事項: "ブートストラップ チュートリアル"]

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="keywords" content="">
    <meta name="description" content="">
    <link href="bootstrap5/bootstrap.min.css" rel="stylesheet">
    <title>滑动窗口</title>
  </head>
  <body>
    <div>
      <br><br><br>
    <div id="accordionExample">
        <div>
        <h2 id="headingOne">
        <button type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
          李白
        </button>
        </h2>
        <div id="collapseOne" class="accordion-collapse collapse show" aria-labelledby="headingOne" data-bs-parent="#accordionExample">
        <div>
          <strong>李白(701年-762年)</strong>  ,字太白,号青莲居士,又号“谪仙人”,是唐代伟大的浪漫主义诗人,被后人誉为“诗仙”。代表作有《望庐山瀑布》《行路难》《蜀道难》《将进酒》《梁甫吟》《早发白帝城》等多首。
        </div>
        </div>
        </div>
        <div>
        <h2 id="headingTwo">
        <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
          杜甫
        </button>
        </h2>
        <div id="collapseTwo" class="accordion-collapse collapse" aria-labelledby="headingTwo" data-bs-parent="#accordionExample">
        <div>
          <strong>杜甫(712年—770年)</strong>
          ,字子美,原籍湖北襄阳,后徙河南巩县。唐代伟大的现实主义诗人,
          与李白合称“李杜”。杜甫在中国古典诗歌中的影响非常深远,被后人称为“诗圣”,他的诗被称为“诗史”。
          杜甫创作了《登高》《春望》《北征》《三吏》《三别》等名作。
        </div>
        </div>
        </div>
        <div>
        <h2 id="headingThree">
        <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
          白居易
        </button>
        </h2>
        <div id="collapseThree" class="accordion-collapse collapse" aria-labelledby="headingThree" data-bs-parent="#accordionExample">
        <div>
          <strong>白居易(772年-846年)</strong> 
          ,字乐天,号香山居士,又号醉吟先生,祖籍山西太原。
          是唐代伟大的现实主义诗人,白居易的诗歌题材广泛,形式多样,语言平易通俗,有“诗魔”和“诗王”之称。
          有《白氏长庆集》传世,代表诗作有《长恨歌》、《卖炭翁》、《琵琶行》等。
        </div>
        </div>
        </div>
        </div>

      </div>
     <script src="bootstrap5/bootstrap.bundle.min.js" ></script>
  </body>
</html>

Bootstrapのアコーディオンコンポーネントの使い方を徹底解説(例)

##2 Accordion コンポーネントの構造

2.1 コンテナ

Accordion コンポーネントアコーディオン コンテナに含める必要があります

<div class="accordion">..</div>

2.2 アコーディオン エントリ

アコーディオン コンポーネントには、上記の例のように多数のエントリがあり、次は 1 つのエントリです。各エントリにはタイトルと内容が含まれます。

<div class="accordion-item">
        <h2 class="accordion-header" id="headingTwo">
        <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
          杜甫
        </button>
        </h2>
        <div id="collapseTwo" class="accordion-collapse collapse" aria-labelledby="headingTwo" data-bs-parent="#accordionExample">
        <div class="accordion-body">
          <strong>杜甫(712年—770年)</strong>
          ,字子美,原籍湖北襄阳,后徙河南巩县。唐代伟大的现实主义诗人,
          与李白合称“李杜”。杜甫在中国古典诗歌中的影响非常深远,被后人称为“诗圣”,他的诗被称为“诗史”。
          杜甫创作了《登高》《春望》《北征》《三吏》《三别》等名作。
        </div>
        </div>
        </div>

2.3 エントリのタイトル

次のコードはエントリのタイトルです。これには、h2 タグとボタンが含まれています。実際に変更する必要があるのは、テキストと

data だけです。次のボタンの -bs -target の値で十分です。

 <h2 class="accordion-header" id="headingTwo">
        <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
          杜甫
        </button>
        </h2>

2.4 エントリの内容

以下のコードがエントリの内容で、同様に一番外側のidと

data-bs-target#の値を対応させるだけで済みます。 ## 。 エントリのコンテンツには、任意の HTML コードとテキスト、リスト、画像などを含めることができます。

<div id="collapseTwo" class="accordion-collapse collapse" aria-labelledby="headingTwo" data-bs-parent="#accordionExample">
        <div class="accordion-body">
          杜甫(712年—770年
          ,字子美,原籍湖北襄阳,后徙河南巩县。唐代伟大的现实主义诗人,
          与李白合称“李杜”。杜甫在中国古典诗歌中的影响非常深远,被后人称为“诗圣”,他的诗被称为“诗史”。
          杜甫创作了《登高》《春望》《北征》《三吏》《三别》等名作。
        </div>
        </div>

3 スタイルの隣

アコーディオンが親コンテナのすぐ隣に配置されるように、デフォルトの背景色、一部の境界線、およびいくつかの丸い角を削除します。 accordion-flush をコンテナに追加するだけです。

<div class="accordion accordion-flush">

次のコードは 2 つのスタイルの違いを示しています。上下左右と下の 4 隅に注目してください。さらに、2 つのアコーディオン コンポーネントは同じページ上で異なる ID を定義する必要があることに注意してください。そうしないと、混乱を制御するのが簡単になります。

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="keywords" content="">
    <meta name="description" content="">
    <link href="bootstrap5/bootstrap.min.css" rel="stylesheet">
    <title>滑动窗口</title>
  </head>
  <body>
    <div>
      <br><br><br>
    <div id="accordionExample">
        <div>
        <h2 id="headingOne">
        <button type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
          李白
        </button>
        </h2>
        <div id="collapseOne" class="accordion-collapse collapse show" aria-labelledby="headingOne" data-bs-parent="#accordionExample">
        <div>
          <strong>李白(701年-762年)</strong>  ,字太白,号青莲居士,又号“谪仙人”,是唐代伟大的浪漫主义诗人,被后人誉为“诗仙”。代表作有《望庐山瀑布》《行路难》《蜀道难》《将进酒》《梁甫吟》《早发白帝城》等多首。
        </div>
        </div>
        </div>
        <div>
        <h2 id="headingTwo">
        <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
          杜甫
        </button>
        </h2>
        <div id="collapseTwo" class="accordion-collapse collapse" aria-labelledby="headingTwo" data-bs-parent="#accordionExample">
        <div>
          <strong>杜甫(712年—770年)</strong>
          ,字子美,原籍湖北襄阳,后徙河南巩县。唐代伟大的现实主义诗人,
          与李白合称“李杜”。杜甫在中国古典诗歌中的影响非常深远,被后人称为“诗圣”,他的诗被称为“诗史”。
          杜甫创作了《登高》《春望》《北征》《三吏》《三别》等名作。
        </div>
        </div>
        </div>
        <div>
        <h2 id="headingThree">
        <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
          白居易
        </button>
        </h2>
        <div id="collapseThree" class="accordion-collapse collapse" aria-labelledby="headingThree" data-bs-parent="#accordionExample">
        <div>
          <strong>白居易(772年-846年)</strong> 
          ,字乐天,号香山居士,又号醉吟先生,祖籍山西太原。
          是唐代伟大的现实主义诗人,白居易的诗歌题材广泛,形式多样,语言平易通俗,有“诗魔”和“诗王”之称。
          有《白氏长庆集》传世,代表诗作有《长恨歌》、《卖炭翁》、《琵琶行》等。
        </div>
        </div>
        </div>
        </div>

        <br><br><br>
    <div class="accordion accordion-flush" id="accordionExample2">
        <div>
        <h2 id="headingOne2">
        <button type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne2" aria-expanded="true" aria-controls="collapseOne">
          李白
        </button>
        </h2>
        <div id="collapseOne2" class="accordion-collapse collapse show" aria-labelledby="headingOne" data-bs-parent="#accordionExample2">
        <div>
          <strong>李白(701年-762年)</strong>  ,字太白,号青莲居士,又号“谪仙人”,是唐代伟大的浪漫主义诗人,被后人誉为“诗仙”。代表作有《望庐山瀑布》《行路难》《蜀道难》《将进酒》《梁甫吟》《早发白帝城》等多首。
        </div>
        </div>
        </div>
        <div>
        <h2 id="headingTwo2">
        <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseTwo2" aria-expanded="false" aria-controls="collapseTwo">
          杜甫
        </button>
        </h2>
        <div id="collapseTwo2" class="accordion-collapse collapse" aria-labelledby="headingTwo" data-bs-parent="#accordionExample2">
        <div>
          <strong>杜甫(712年—770年)</strong>
          ,字子美,原籍湖北襄阳,后徙河南巩县。唐代伟大的现实主义诗人,
          与李白合称“李杜”。杜甫在中国古典诗歌中的影响非常深远,被后人称为“诗圣”,他的诗被称为“诗史”。
          杜甫创作了《登高》《春望》《北征》《三吏》《三别》等名作。
        </div>
        </div>
        </div>
        <div>
        <h2 id="headingThree2">
        <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseThree2" aria-expanded="false" aria-controls="collapseThree">
          白居易
        </button>
        </h2>
        <div id="collapseThree2" class="accordion-collapse collapse" aria-labelledby="headingThree" data-bs-parent="#accordionExample2">
        <div>
          <strong>白居易(772年-846年)</strong> 
          ,字乐天,号香山居士,又号醉吟先生,祖籍山西太原。
          是唐代伟大的现实主义诗人,白居易的诗歌题材广泛,形式多样,语言平易通俗,有“诗魔”和“诗王”之称。
          有《白氏长庆集》传世,代表诗作有《长恨歌》、《卖炭翁》、《琵琶行》等。
        </div>
        </div>
        </div>
        </div>
        
        
      </div>
     <script src="bootstrap5/bootstrap.bundle.min.js" ></script>
  </body>
</html>

Bootstrapのアコーディオンコンポーネントの使い方を徹底解説(例)4 アコーディオン コンポーネントの使用リスト

アコーディオン コンポーネントのエントリ コンテンツはリストにすることができ、通常はバックグラウンド ナビゲーション パネルとフロントエンド側で使用されます。折りたたみニュース。 text ジェネリック クラスを使用して、テキストの配置をフォーマットできます。または、css を使用してリスト表示のスタイルを再定義します。

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="keywords" content="">
    <meta name="description" content="">
    <link href="bootstrap5/bootstrap.min.css" rel="stylesheet">
    <title>手风琴组件</title>
  </head>
  <body>
    <div>
      <br><br><br>
    <div id="accordionExample">
        <div>
        <h2 id="headingOne">
        <button type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
          文章管理
        </button>
        </h2>
        <div id="collapseOne" class="accordion-collapse collapse show" aria-labelledby="headingOne" data-bs-parent="#accordionExample">
        <div>
          <ul>
              <li>分类管理</li>
              <li>文章列表</li>
              <li>添加文章</li>
          </ul>
        </div>
        </div>
        </div>
        <div>
        <h2 id="headingTwo">
        <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
          网站管理
        </button>
        </h2>
        <div id="collapseTwo" class="accordion-collapse collapse" aria-labelledby="headingTwo" data-bs-parent="#accordionExample">
        <div>
            <ul>
                <li>网站设置</li>
                <li>风格选择</li>
                <li>插件管理</li>
            </ul>
        </div>
        </div>
        </div>
        <div>
        <h2 id="headingThree">
        <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
          会员管理
        </button>
        </h2>
        <div id="collapseThree" class="accordion-collapse collapse" aria-labelledby="headingThree" data-bs-parent="#accordionExample">
        <div>
            <ul>
                <li>普通会员</li>
                <li>VIP会有</li>
            </ul>
        </div>
        </div>
        </div>
        </div>

      </div>
     <script src="bootstrap5/bootstrap.bundle.min.js" ></script>
  </body>
</html>

Bootstrapのアコーディオンコンポーネントの使い方を徹底解説(例)ブートストラップの詳細については、

ブートストラップの基本チュートリアル

を参照してください。 !

以上がBootstrapのアコーディオンコンポーネントの使い方を徹底解説(例)の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はjuejin.cnで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。