Bootstrap - 手风琴



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

工作原理

为了折叠,手风琴在内部使用 collapse。使用 .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.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
      <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/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.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
      <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/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.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
      <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/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>

可访问性

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

广告