Bootstrap - 手风琴


本章将讨论 Bootstrap 手风琴。手风琴垂直折叠元素,通过类更改显示和隐藏。使用折叠 JavaScript 插件创建垂直折叠的手风琴。

怎么运行的

为了折叠,手风琴内部使用了折叠。使用.open类到.accordion元素来渲染展开的手风琴。

基本示例

要展开和折叠内容,请单击下面的手风琴。

您可以使用编辑和运行选项编辑并尝试运行此代码。

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <title>Bootstrap - Accordion</title>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
      <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
      <style>
      </style>
    </head>
    <body>
      <div class="accordion" id="basicAccordion">
        <div class="accordion-item">
          <h2 class="accordion-header">
            <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#firstCollapse" aria-expanded="true" aria-controls="firstCollapse">
             Bootstrap - Accordian
            </button>
          </h2>
          <div id="firstCollapse" class="accordion-collapse collapse show" data-bs-parent="#basicAccordion">
            <div class="accordion-body">
              The <b>accordion</b> collapses elements vertically, shown and hidden by class change.
            </div>
          </div>
        </div>
        <div class="accordion-item">
          <h2 class="accordion-header">
            <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#secondCollapse" aria-expanded="false" aria-controls="secondCollapse">
              Bootstrap - Buttons
            </button>
          </h2>
          <div id="secondCollapse" class="accordion-collapse collapse" data-bs-parent="#basicAccordion">
            <div class="accordion-body">
              You can use Bootstrap’s custom button styles for actions in forms, dialogs and many more.        
            </div>
          </div>
        </div>
      </div>
    </body>
    </html>

冲洗

要渲染边到边的手风琴,请使用.accordion-flush删除边框和圆角。

您可以使用编辑和运行选项编辑并尝试运行此代码。

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <title>Bootstrap - Accordion</title>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
      <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
      <style>
      </style>
    </head>
    <body>
      <div class="accordion accordion-flush" id="flushAccordion">
        <div class="accordion-item">
          <h2 class="accordion-header">
            <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse"
              data-bs-target="#flushCollapseOne" aria-expanded="false" aria-controls="flushCollapseOne">
              Bootstrap - Accordian
            </button>
          </h2>
          <div id="flushCollapseOne" class="accordion-collapse collapse" data-bs-parent="#flushAccordion">
            <div class="accordion-body">
              The <b>accordion</b> collapses elements vertically, shown and hidden by class change.
            </div>
          </div>
        </div>
        <div class="accordion-item">
          <h2 class="accordion-header">
            <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse"
              data-bs-target="#flushCollapseTwo" aria-expanded="false" aria-controls="flushCollapseTwo">
              Bootstrap - Buttons
            </button>
          </h2>
          <div id="flushCollapseTwo" class="accordion-collapse collapse" data-bs-parent="#flushAccordion">
            <div class="accordion-body">
              You can use Bootstrap’s custom button styles for actions in forms, dialogs and many more.
            </div>
          </div>
        </div>
      </div>
      </div>
    </body>
    </html>

始终开放

要在打开另一个项目时保持折叠项目打开,请从每个.accordion-collapse中删除data-bs-parent属性。

您可以使用编辑和运行选项编辑并尝试运行此代码。

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <title>Bootstrap - Accordion</title>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
      <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
      <style>
      </style>
    </head>
    <body>
      <div class="accordion" id="alwaysOpenAccordion">
        <div class="accordion-item">
          <h2 class="accordion-header">
            <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#firstCollpaseofAlwaysOpen" aria-expanded="true" aria-controls="firstCollpaseofAlwaysOpen">
              Bootstrap - Accordian
            </button>
          </h2>
          <div id="firstCollpaseofAlwaysOpen" class="accordion-collapse collapse show">
            <div class="accordion-body">
              The <b>accordion</b> collapses elements vertically, shown and hidden by class change.
            </div>
          </div>
        </div>
        <div class="accordion-item">
          <h2 class="accordion-header">
            <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#secondCollpaseofAlwaysOpen" aria-expanded="false" aria-controls="secondCollpaseofAlwaysOpen">
              Bootstrap - Buttons
            </button>
          </h2>
          <div id="secondCollpaseofAlwaysOpen" class="accordion-collapse collapse">
            <div class="accordion-body">
              The <b>accordion</b> collapses elements vertically, shown and hidden by class change.
            </div>
          </div>
        </div>
      </div>
    </body>
    </html>

无障碍

有关详细信息,请参阅折叠可访问性部分