4.9Bootstrap学习js插件篇之折叠

来源:互联网 发布:php array split 编辑:程序博客网 时间:2024/04/29 01:33

关于

对为支持折叠功能的组件,例如accordions和导航,赋予基本样式和灵活的支持。

插件依赖

折叠插件依赖过渡效果插件。

案例

使用折叠插件,通过扩展panel组件从而构建了一个简单的accordion组件。

Collapsible Group Item #1

Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.

Collapsible Group Item #2

Collapsible Group Item #3

<div class="panel-group" id="accordion">  <div class="panel panel-default">    <div class="panel-heading">      <h4 class="panel-title">        <a data-toggle="collapse" data-toggle="collapse" data-parent="#accordion" href="#collapseOne">          Collapsible Group Item #1        </a>      </h4>    </div>    <div id="collapseOne" class="panel-collapse collapse in">      <div class="panel-body">        Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.      </div>    </div>  </div>  <div class="panel panel-default">    <div class="panel-heading">      <h4 class="panel-title">        <a data-toggle="collapse" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">          Collapsible Group Item #2        </a>      </h4>    </div>    <div id="collapseTwo" class="panel-collapse collapse">      <div class="panel-body">        Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.      </div>    </div>  </div>  <div class="panel panel-default">    <div class="panel-heading">      <h4 class="panel-title">        <a data-toggle="collapse" data-toggle="collapse" data-parent="#accordion" href="#collapseThree">          Collapsible Group Item #3        </a>      </h4>    </div>    <div id="collapseThree" class="panel-collapse collapse">      <div class="panel-body">        Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.      </div>    </div>  </div></div>

You can also use the plugin without the accordion markup. Make a button toggle the expanding and collapsing of another element.

<button type="button" class="btn btn-danger" data-toggle="collapse" data-target="#demo">  simple collapsible</button><div id="demo" class="collapse in">...</div>

预览效果:


用法

The collapse plugin utilizes a few classes to handle the heavy lifting:

  • .collapse hides the content
  • .collapse.in shows the content
  • .collapsing is added when the transition starts, and removed when it finishes

These classes can be found in component-animations.less.

通过data属性

仅仅通过向页面元素添加data-toggle="collapse" 和data-target就可以为其赋予控制可折叠页面元素的能力。data-target属性接受一个CSS选择器作为其控制对象。请确保为可折叠页面元素添加collapse class。如果你希望可折叠页面元素的默认状态是展开的,请添加in class。

为了给一组可折叠页面元素添加控制器,添加data-parent="#selector"即可。请参考下面给出的案例。

通过JavaScript

手动触发:

$('.collapse').collapse()

代码:

<button type="button" class="btn btn-danger" onClick="Open()">打开</button><button type="button" class="btn btn-danger" onClick="Hide()">折叠</button><div id="demo1" class="collapse in">Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunc</div>

预览:


选项

可以将选项通过data属性或JavaScript传递。对于data属性,需要将选项名称放到data-之后,例如data-parent=""

名称类型默认值描述parentselectorfalseIf selector then all collapsible elements under the specified parent will be closed when this collapsible item is shown. (similar to traditional accordion behavior - this dependent on theaccordion-group class)togglebooleantrueToggles the collapsible element on invocation

方法

.collapse(options)

赋予页面元素可折叠功能。接受一个可选的object作为参数。

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

.collapse('toggle')

展示或隐藏一个可折叠的页面元素。

.collapse('show')

展示一个可折叠页面元素。

.collapse('hide')

隐藏一个可折叠页面元素。

事件

Bootstrap中的折叠插件对外暴露了一组可以监听的事件。

事件类型描述show.bs.collapse当show方法被调用之后立即触发此事件。shown.bs.collapse当可折叠页面元素显示出来之后(同时CSS过渡效果也已执行完毕),此事件被触发。hide.bs.collapse当hide方法被调用之后,此事件被立即触发。hidden.bs.collapse当可折叠页面元素隐藏之后(同时CSS过渡效果也已执行完毕),此事件被触发。
$('#myCollapsible').on('hidden.bs.collapse', function () {  // do something…})
0 0