Bootstrap折叠

Bootstrap折叠

基本折叠

折叠可用来显示和隐藏内容



<div id="demo" class="collapse">
    被折叠的区域
div>

元素,折叠实例如下:

<a href="#demo" data-toggle="collapse">折叠a>
<div id="demo" class="collapse">
    被折叠的元素
div>

默认情况下,折叠的内容是隐藏的,可添加.in类,来展示内容

<a href="#demo" data-toggle="collapse">折叠a>
<div id="demo" class="collapse in">
    被折叠的元素
div>

可折叠面板

    <div class="panel-group">
        <div class="panel panel-default">
            <div class="panel-heading">
                

class="panel-title"> "collapse" href="#collapse1">可折叠面板

div> <div id="collapse1" class="panel-collapse collapse"> <div class="panel-body">Panel Bodydiv> <div class="panel-footer">Panel Footerdiv> div> div> div>

效果如下:

Bootstrap折叠_第1张图片

可折叠List Group

<div class="panel-group">
  <div class="panel panel-default">
    <div class="panel-heading">
      <h4 class="panel-title">
        <a data-toggle="collapse" href="#collapse1">Collapsible list groupa>
      h4>
    div>
    <div id="collapse1" class="panel-collapse collapse">
      <ul class="list-group">
        <li class="list-group-item">Oneli>
        <li class="list-group-item">Twoli>
        <li class="list-group-item">Threeli>
      ul>
      <div class="panel-footer">Footerdiv>
    div>
  div>
div>

效果如下:

Bootstrap折叠_第2张图片

手风琴效果

<div class="panel-group" id="accordion">
  <div class="panel panel-default">
    <div class="panel-heading">
      

class="panel-title"> "collapse" data-parent="#accordion" href="#collapse1"> Collapsible Group 1

div> <div id="collapse1" class="panel-collapse collapse in"> <div class="panel-body">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.div> div> div> <div class="panel panel-default"> <div class="panel-heading">

class="panel-title"> "collapse" data-parent="#accordion" href="#collapse2"> Collapsible Group 2

div> <div id="collapse2" class="panel-collapse collapse"> <div class="panel-body">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.div> div> div> <div class="panel panel-default"> <div class="panel-heading">

class="panel-title"> "collapse" data-parent="#accordion" href="#collapse3"> Collapsible Group 3

div> <div id="collapse3" class="panel-collapse collapse"> <div class="panel-body">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.div> div> div> div>

效果如下:

Bootstrap折叠_第3张图片

data-parent表示当可折叠项目显示时,指定父元素下的所有可折叠的元素将被关闭,就是打开一个,其它的关闭,类似于手风琴

Bootstrap JS Collapse

一些方法:

1.激活内容为可折叠元素。接受一个可选的 options 对象

$('#identifier').collapse({
    toggle: false
}

2.切换显示/隐藏可折叠元素

$('#identifier').collapse('toggle')

3.显示可折叠元素

$('#identifier').collapse('show')

4.隐藏可折叠元素

$('#identifier').collapse('hide')

事件

1.show.bs.collapse在调用 show 方法后触发该事件
2.shown.bs.collapse 当折叠元素对用户可见时触发该事件(将等待 CSS 过渡效果完成)
3.hide.bs.collapse当调用 hide 实例方法时立即触发该事件
4.hidden.bs.collapse当折叠元素对用户隐藏时触发该事件(将等待 CSS 过渡效果完成)

如下的例子
1.button展开和折叠对应不同的icon

<div class="container">
    <h2>Expand and Collapse with different iconsh2>
    <button type="button" class="btn btn-success" data-toggle="collapse" data-target="#demo">
        <span class="glyphicon glyphicon-collapse-down">span> Open
    button>
    <div id="demo" class="collapse">
        被折叠/打开的内容
    div>
div>

<script>
    $(document).ready(function(){
        $("#demo").on("hide.bs.collapse", function(){
            $(".btn").html(' Open');
        });
        $("#demo").on("show.bs.collapse", function(){
            $(".btn").html(' Close');
        });
    });
script>

如下的展开和折叠状态:

4
5

也可以使用css使用

  <style>
  /* 可折叠内容展示的icon */
  .btn:after {
    font-family: "Glyphicons Halflings";
    content: "\e114";
    float: right;
    margin-left: 15px;
  }
  /* 可折叠内容隐藏的icon */
  .btn.collapsed:after {
    content: "\e080";
  }
style>

<div class="container">
  <h2>Simple Collapsibleh2>
  <button type="button" class="btn btn-lg btn-info collapsed" data-toggle="collapse" data-target="#demo">Simple collapsiblebutton>
  <div id="demo" class="collapse">
    Lorem ipsum dolor sit amet, consectetur adipisicing elit,
    sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
    quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
  div>
div>

效果如下:

6
7

你可能感兴趣的:(Bootstrap)