Bootstrap5 折叠(Collapse)

半兽人 发表于: 2020-07-04   最后更新时间: 2021-04-13 15:36:14  
{{totalSubscript}} 订阅,1190 游览

折叠

通过一些类和JavaScript插件来切换内容的可见性。

例子

点击下面的按钮,通过改变类来显示和隐藏另一个元素。

  • .collapse 隐藏内容
  • .collapsing 过渡期间
  • .collapse.show 显示内容

你可以使用带有href属性的链接,或带有data-target属性的按钮。这两种方式,都需要data-toggle="collapse"

<p>
  <a class="btn btn-primary" data-bs-toggle="collapse" href="#collapseExample" role="button" aria-expanded="false" aria-controls="collapseExample">
    Link with href
  </a>
  <button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target="#collapseExample" aria-expanded="false" aria-controls="collapseExample">
    Button with data-bs-target
  </button>
</p>
<div class="collapse" id="collapseExample">
  <div class="card card-body">
    Some placeholder content for the collapse component. This panel is hidden by default but revealed when the user activates the relevant trigger.
  </div>
</div>

在线运行

多个目标

<button><a>可以通过在其hrefdata-target属性中使用选择器引用它们来显示和隐藏多个元素。多个<button><a>可以显示和隐藏一个元素,如果它们都用hrefdata-target属性来引用它。

<p>
  <a class="btn btn-primary" data-bs-toggle="collapse" href="#multiCollapseExample1" role="button" aria-expanded="false" aria-controls="multiCollapseExample1">Toggle first element</a>
  <button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target="#multiCollapseExample2" aria-expanded="false" aria-controls="multiCollapseExample2">Toggle second element</button>
  <button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target=".multi-collapse" aria-expanded="false" aria-controls="multiCollapseExample1 multiCollapseExample2">Toggle both elements</button>
</p>
<div class="row">
  <div class="col">
    <div class="collapse multi-collapse" id="multiCollapseExample1">
      <div class="card card-body">
        Some placeholder content for the first collapse component of this multi-collapse example. This panel is hidden by default but revealed when the user activates the relevant trigger.
      </div>
    </div>
  </div>
  <div class="col">
    <div class="collapse multi-collapse" id="multiCollapseExample2">
      <div class="card card-body">
        Some placeholder content for the second collapse component of this multi-collapse example. This panel is hidden by default but revealed when the user activates the relevant trigger.
      </div>
    </div>
  </div>
</div>

在线运行



您需要解锁本帖隐藏内容请: 点击这里
本帖隐藏的内容



提问