当前位置: 首页 > news >正文

【Bootstrap V4系列】学习入门教程之 组件-折叠(Collapse)

Bootstrap V4系列 学习入门教程之 组件-折叠(Collapse)

  • 折叠(Collapse)
    • How it works
    • 一、Example
    • 二、Horizontal 水平的
    • 三、Multiple targets 多个目标
    • 四、Accordion example 手风琴示例

折叠(Collapse)

通过几个类和我们的JavaScript插件来切换项目中内容的可见性。

How it works

collapse JavaScript插件用于显示和隐藏内容。按钮或锚点用作触发器,映射到您切换的特定元素。折叠元素将使height 高度从其当前值动画化为0。考虑到CSS处理动画的方式,您不能在.collapse 元素上使用padding填充。相反,将类用作独立的包装元素。

一、Example

单击下面的按钮,通过类更改显示和隐藏另一个元素:

  • .collapse hides content (隐藏内容)
  • .collapsing is applied during transitions (在转换过程中应用折叠)
  • .collapse.show shows content (显示内容)

通常,我们建议使用带有data-target属性的按钮。虽然从语义角度来看不建议使用,但您也可以使用带有href属性的链接(以及role=“button”)。在这两种情况下,都需要数据切换data-toggle="collapse"

<!--Example-->
<!--单击下面的按钮,通过类更改显示和隐藏另一个元素-->
<p><a class="btn btn-primary" data-toggle="collapse" href="#collapseExample" role="button" aria-expanded="false" aria-controls="collapseExample">Link with href</a><button class="btn btn-primary" type="button" data-toggle="collapse" data-target="#collapseExample" aria-expanded="false" aria-controls="collapseExample">Button with data-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>

页面展示效果:

在这里插入图片描述

点击展开(显示内容)后的效果:

在这里插入图片描述

二、Horizontal 水平的

折叠插件还支持水平折叠。添加.width修饰符类以转换宽度而不是高度,并在直接的子元素上设置宽度。

请注意,虽然下面的示例设置了最小高度以避免在我们的文档中过度重新绘制,但这并不是明确要求的。只需要子元素上的宽度。

<!--水平的 Horizontal-->
<p><button class="btn btn-primary" type="button" data-toggle="collapse" data-target="#collapseWidthExample" aria-expanded="false" aria-controls="collapseWidthExample">Toggle width collapse</button>
</p>
<div style="min-height: 120px;"><div class="collapse width" id="collapseWidthExample"><div class="card card-body" style="width: 320px;">This is some placeholder content for a horizontal collapse. It's hidden by default and shown when triggered.</div></div>
</div>

页面展示效果:

在这里插入图片描述

点击展开(显示内容)后的效果:
在这里插入图片描述

三、Multiple targets 多个目标

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

<!--Multiple targets-->
<p><a class="btn btn-primary" data-toggle="collapse" href="#multiCollapseExample1" role="button" aria-expanded="false" aria-controls="multiCollapseExample1">Toggle first element</a><button class="btn btn-primary" type="button" data-toggle="collapse" data-target="#multiCollapseExample2" aria-expanded="false" aria-controls="multiCollapseExample2">Toggle second element</button><button class="btn btn-primary" type="button" data-toggle="collapse" data-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>

页面展示效果:
在这里插入图片描述
点击展开1(显示内容)后的效果:

在这里插入图片描述
点击展开2(显示内容)后的效果:
在这里插入图片描述
点击展开1和2(显示内容)后的效果:
在这里插入图片描述

四、Accordion example 手风琴示例

使用卡片组件,可以扩展默认折叠行为以创建手风琴。要正确实现手风琴风格,一定要使用.accordion作为包装。

<!--手风琴示例 Accordion example-->
<div class="accordion" id="accordionExample"><div class="card"><div class="card-header" id="headingOne"><h2 class="mb-0"><button class="btn btn-link btn-block text-left" type="button" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">Collapsible Group Item #1</button></h2></div><div id="collapseOne" class="collapse show" aria-labelledby="headingOne" data-parent="#accordionExample"><div class="card-body">Some placeholder content for the first accordion panel. This panel is shown by default, thanks to the <code>.show</code> class.</div></div></div><div class="card"><div class="card-header" id="headingTwo"><h2 class="mb-0"><button class="btn btn-link btn-block text-left collapsed" type="button" data-toggle="collapse" data-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">Collapsible Group Item #2</button></h2></div><div id="collapseTwo" class="collapse" aria-labelledby="headingTwo" data-parent="#accordionExample"><div class="card-body">Some placeholder content for the second accordion panel. This panel is hidden by default.</div></div></div><div class="card"><div class="card-header" id="headingThree"><h2 class="mb-0"><button class="btn btn-link btn-block text-left collapsed" type="button" data-toggle="collapse" data-target="#collapseThree" aria-expanded="false" aria-controls="collapseThree">Collapsible Group Item #3</button></h2></div><div id="collapseThree" class="collapse" aria-labelledby="headingThree" data-parent="#accordionExample"><div class="card-body">And lastly, the placeholder content for the third and final accordion panel. This panel is hidden by default.</div></div></div>
</div>

页面展示效果:

在这里插入图片描述
点击展开2(显示内容)后的效果:

在这里插入图片描述
点击展开3(显示内容)后的效果:

在这里插入图片描述

相关文章:

  • mysql修改root密码
  • C++20新特新——02特性的补充
  • 性能比拼: Redis Streams vs Pub/Sub
  • 解决使用lettuce连接Redis超时的问题(tcpUserTimeout 参数失效问题)
  • YOLOv1:开创实时目标检测新纪元
  • Wireshark抓账号密码
  • 普通笔记本与军用加固笔记本电脑的区别,探索防水、防爆、防摔的真·移动工作站!
  • 在线PDF阅读方案:jQuery + PDF.js
  • 内网和外网怎么互通?外网访问内网的几种简单方式
  • 解决HomeAssistant 无法安装 samba share问题
  • 【数据库原理及安全实验】实验六 角色访问控制
  • 《C++ Templates》:有关const、引用、指针的一些函数模板实参推导的例子
  • C#——NET Core 中实现汉字转拼音
  • Redis 8.0 正式版发布,新特性很强!
  • 品质领航家装时代,亚新丽以匠心雕琢每一寸美好
  • 5.6-DAE实现
  • AI大模型分类以及Prompt优化技巧
  • 迁移学习:如何加速模型训练和提高性能
  • Vue3 自定义指令的原理,以及应用
  • Make:独立创造者手册——从0到1的商业自由之路
  • 当创业热土遇上年轻气息,上海南汇新城发展如何再发力?
  • 新华每日电讯:给“男性妇科病论文”开一剂复方药
  • 英国和美国就关税贸易协议条款达成一致
  • 司法部:建立行政执法监督企业联系点,推行行政执法监督员制度
  • 两部门发布外汇领域行刑反向衔接案例,织密金融安全“防护网”
  • 中国德国商会报告:76%在华德企受美国关税影响,但对华投资战略依然稳固